Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Exception handling, DLL and RTTI ??

Posted on 2000-04-13
10
Medium Priority
?
541 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1000 total points
ID: 2733501
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

636 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