Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 340
Brian R. Bondy Avatar asked Aug 30 '08 16:08

Brian R. Bondy


People also ask

When an exception object is thrown it is caught by?

When exceptions are thrown, they may be caught by the application code. The exception class extends Throwable . The constructor contains two parameters: message and cause.

Why do we need exception handling in C#?

Exception Handling in C# is a process to handle runtime errors. We perform exception handling so that normal flow of the application can be maintained even after runtime errors. In C#, exception is an event or object which is thrown at runtime. All exceptions the derived from System.

When unhandled exception is generated the program will?

An exception is an unscheduled interruption of service in a program. If you don't have any means of catching or trapping exceptions then they become unhandled and will cause program failure. Major errors can occur if your code divides by zero or tries to reference a value that doesn't exist.


2 Answers

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

like image 148
Mark Ransom Avatar answered Oct 13 '22 01:10

Mark Ransom


You pointed to a breakpoint in the code. Since you are in the debugger, you could set a breakpoint on the constructor of the exception class, or set Visual Studio debugger to break on all thrown exceptions (Debug->Exceptions Click on C++ exceptions, select thrown and uncaught options)

like image 27
Jeremy Avatar answered Oct 13 '22 00:10

Jeremy