Alex Rivera | Logout

Finding out the source of an exception in C++ after it is caught?

Asked 2008-08-30T16:16:32.207
13

I'm looking for an answer in MS VC++.

When debugging a large C++ application, which unfortunately has a very extensive usage of C++ exceptions. Sometimes I catch an exception a little later than I actually want.

Example in pseudo code:

FunctionB()
{
    ...
    throw e;
    ...
}

FunctionA()
{
    ...
    FunctionB()
    ...
}

try
{
    Function A()
}
catch(e)
{
    (<--- breakpoint)
    ...
}

I can catch the exception with a breakpoint when debugging. But I can't trace back if the exception occurred in FunctionA() or FunctionB(), or some other function. (Assuming extensive exception use and a huge version of the above example).

One solution to my problem is to determine and save the call stack in the exception constructor (i.e. before it is caught). But this would require me to derive all exceptions from this base exception class. It would also require a lot of code, and perhaps slow down my program.

Is there an easier way that requires less work? Without having to change my large code base?

Are there better solutions to this problem in other languages?

Edit
Report

3 Answers

5

Put a breakpoint in the exception object constructor. You'll get your breakpoint before the exception is thrown.

answered 2008-10-24T16:44:20.230
1

If you're debugging from the IDE, go to Debug->Exceptions, click Thrown for C++ exceptions.

answered 2008-09-05T20:40:04.970
0

I use my own exceptions. You can handle them quite simple - also they contain text. I use the format:

throw Exception( "comms::serial::serial( )", "Something failed!" );

Also I have a second exception format:

throw Exception( "comms::serial::serial( )", ::GetLastError( ) );

Which is then converted from a DWORD value to the actual message using FormatMessage. Using the where/what format will show you what happened and in what function.

answered 2008-08-30T23:27:36.693

Your Answer