throw

C/C++ Reference

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. For example:

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