Solved

Exception handling, DLL and RTTI ??

Posted on 2000-04-13
10
531 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

816 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

11 Experts available now in Live!

Get 1:1 Help Now