Showing posts with label Exception Handling. Show all posts
Showing posts with label Exception Handling. Show all posts

Tuesday, May 11, 2010

Unexpected Exception

How to specify Exception?
 func()                    // this can throw any Excpeption
 func()  throw()      // this Will NOT throw any Excpeption
 func()  throw (bad_alloc , X )  // this can throw only specified  Excpeption

unexpected() is called when function throws exception other  than specified 

It uses     _unexpected_handler  which can be changed  
unexpected_handler old_hand  = set_unexpected( newfunction);
 
void newfunction( ) 
{
   cout << "Hi" << endl;
   terminate( );
}
 

Saturday, May 8, 2010

Exception Handling

Q How to catch all type of Exception ?
Ans Three dots try { } catch(...)
three dots is called   Ellipsis

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

If we throw Derived object will it catch by base or derived ? see code and Answer


Output :
base cought

NEXT Question comes in Mind is  : 
What if inheritance is of PRIVATE or PROTECTED
Ans: in that case object will be cought at Derived catch block only.

Remember : Only Public inheritance is " IS-A "relation ., can be treated as BASE TYPE  

Followers