Solved

Exception handling, DLL and RTTI ??

Posted on 2000-04-13
10
534 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
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!

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

726 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