throw

Syntax:

    try {
    statement list;
    }
    catch( typeA arg ) {
    statement list;
    }
    catch( typeB arg ){
    statement list;
    }
    ...
    catch( typeN arg ) {
    statement list;
    }

The throw statement is part of the C++ mechanism for exception handling. This statement, together with the try and catch statements, the C++ exception handling system gives programmers an elegant mechanism for error recovery.

You will generally use a try block to execute potentially error-prone code. Somewhere in this code, a throw statement can be executed, which will cause execution to jump out of the try block and into one of the catch blocks.

A

catch (...)
{
}

will catch any throw without considering what kind of object was thrown and without giving access to the thrown object.

Writing

throw

Within a catch block will re throw what ever was caught.

Example:

     try {
       cout << "Before throwing exception" << endl;
       throw 42;
       cout << "Shouldn't ever see this!" << endl;
     }
     catch( int error ) {
       cerr << "Error: caught exception " << error << endl;
     }

Related Topics: catch, try