Solved

Exception handling, DLL and RTTI ??

Posted on 2000-04-13
10
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

737 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