Exception Handlers

When working with exception handlers, you can set a breakpoint at the appropriate line to determine if an exception is thrown. In addition, you can set breakpoints in these functions that are part of the C++ library support for exceptions:

terminate
Gains control when any unhandled exception occurs, which will result in program termination.
unexpected
Gains control when a function containing an exception specification tries to throw an exception that is not included in that specification.

Note:

You can overwrite terminate and unexpected with your own exception handlers by using set_terminate and set_unexpected functions. This technique can be useful for debugging.

These special library functions are illustrated using the following source:

 

(idb) list 30:29

     30 // Throw an exception.  The "throw(int)" syntax tells the compiler that

     31 // only integer exceptions can escape this method.  This will result in

     32 // an unexpected exception from C++.

     33 //

     34 void throwAnException() throw(int)

     35 {

     36     throw "Bug";

     37 }

     38

     39 // Provide some depth to the stack, for demonstration purposes

     40 //

     41 void someOperation()

     42 {

     43     int z = unalignedAccess();  // Some tests ignore this exception

     44     throwAnException();

     45 }

     46

     47 main()

     48 {

     49     try {

     50         someOperation();

     51     }

     52     catch(char* str) {

     53         std::cout << "Caught exception [" << str << "]" << std::endl;

     54     }

     55     catch(...) {

     56         std::cout << "Caught something" << std::endl;

     57     }

     58 }

 

You can trace the flow of execution, as in the following:

 

(idb) stop at 52

[#1: stop at "x_signals.cxx":52]

(idb) stop in all terminate

Symbol "terminate" is not defined.

No value for expression terminate

Warning: Breakpoint not set

(idb) stop in all unexpected

Symbol "unexpected" is not defined.

No value for expression unexpected

Warning: Breakpoint not set

(idb) run

Caught something

Process has exited with status 0

(idb) where

The "where" command has failed because there is no running program.

(idb) cont

The "cont" command has failed because there is no running program.

(idb) where

The "where" command has failed because there is no running program.

(idb) cont

The "cont" command has failed because there is no running program.

(idb) where

The "where" command has failed because there is no running program.

(idb) cont

The "cont" command has failed because there is no running program.