Solved

Exception handling, DLL and RTTI ??

Posted on 2000-04-13
10
529 Views
Last Modified: 2008-03-10
I am using Borland C++ Builder 4 and implementing a class library that has to be distributed as a set of .dll's.

I am using the generated import libraries to load the dll's at startup.

I am throwing an exception (of a class exported from general.dll and derived of stl exception) from library.dll and trying to catch this in app.exe when that calls a function in library.dll that causes this exception to be thrown ...

Problem is that the exception falls through my catch blocks into the catch for the base class (exception).

I have :
try
{
   libFunction();
}
catch(const myException & E)
{
   ShowMessage("Correct exception caught");
}
catch(const exception& E)
{
   ShowMessage("Base exception caught");
   ShowMessage(AnsiString("Class caught had name :") + typeid(E).name());
}
catch(...)
{
   ShowMessage("Unexpected exception caught");
}

and I always end up in the base exception handler, with the name displayed being  "myException".

It seems like the RTTI for exceptions is done differently from the RTTI provided via the typeid mechanism.

Question(s) :
1)Any ideas how the RTTI is done in by Borland and why this is failing so miserably?
2)Has anyone else ever had similar problems using custom exceptions and dll's?
0
Comment
Question by:gysbert1
  • 5
  • 2
  • 2
  • +1
10 Comments
 

Expert Comment

by:Ofer_E
ID: 2714282
I don't have a compiler on this PC,
but try this (no pun intended):

You see, a catch is not really a
function, and is not defined like one.

class division_by_zero();

void libFunction()
{
 //does something, then fails...
 throw division_by_zero;
}

try
{
   libFunction();
}
catch(division_by_zero)
{
ShowMessage("Correct exception caught");
}
0
 

Expert Comment

by:Ofer_E
ID: 2714285
Ofer_E changed the proposed answer to a comment
0
 
LVL 2

Author Comment

by:gysbert1
ID: 2714840
I realize that but you can also throw a specific class if something goes wrong in stead of only catching CPU exceptions and OS exceptions that are system generated!

That is the whole beauty of using exception handling!

Lesson 1:
You can do the following:

int main(void)
{
  try
  {
    if (getPassword())
    {
      throw (int)1;
    }
    else
    {
       throw (char*)"Nothing is wrong!";
    }
  }
  catch(int& i)
  {
     cout << i;  // This prints 1
  }
  catch(char *message)
  {
     cout <<  message; // This prints the message
  }
}


}
0
 
LVL 2

Author Comment

by:gysbert1
ID: 2721856
Adjusted points from 200 to 500
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2732577
Could you show the line which throws exception?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 2

Author Comment

by:gysbert1
ID: 2733139
I have a static member function in the .dll throwing the exception.

I have in the mean time sorted the problem out by compiling my code with C++ Builder 5. The problem auto-magically went away giving me LOADS of confidence in compilers now!
0
 
LVL 2

Author Comment

by:gysbert1
ID: 2733149
If someone can give me a good reference to somewhere I can find deep down technical detail on how exception handling is supposed to be implemented by the compiler he can have the points anyhow.

Is the RTTI supposed to be used by the exception handling code?

What is the use of throw specs?

When is copies of the exception class supposed to be made and where to?

How and when is the stack supposed to be cleaned up?
0
 
LVL 3

Accepted Solution

by:
ufolk123 earned 500 total points
ID: 2733501
0
 
LVL 3

Expert Comment

by:ufolk123
ID: 2733513
0
 
LVL 2

Author Comment

by:gysbert1
ID: 2733725
Sad to give away 500 points for free! I had to give it to someone to keep the solution on EE though so no sweat.

The Borland reference os pretty good, the other ones say little in a lot of words (in my opinion of course)

The following link has got loads of info on exception  handling to all the detail you should want without being compiler specific:

http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/welcome/dsmsdn/deep051099.htm

ufolk123,
sorry for the low grade but clearly not all things in life are free ...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now