Monday, April 26, 2010

Unhandeled Exceptions in C++ terminate

If a Exception is not handled With proper catch Block of same type or  catch(...) is also not there terminate run-time function is called Which calls abort( )   internally .
To Avoid this we can provide our own Termination Handler function  like:

terminate_handler  set_terminate( term_function );
 
This can be  done  any where in the code . BEFORE EXCEPTION IS THROWN.
Also only one Function can be registered for as Handler
_uncaught_handler
Multiple calls of  set_terminate() overwrites previous value.

OUTPUT :
terminate called after throwing an instance of 'char const*'
Aborted
Note : set_terminate is after exception is thrown.

OUTPUT:
Second Handler was called by terminate
Note :first  handler is overwritten

2 comments:

Followers