Throwing Exceptions


Example

The example shown below illustrates several points. The outer try block has three associated handlers:

Successive handlers are tried in order (i.e. their types are checked to see if they match the type of that which was thrown). The outer try block contains an inner try-catch block. The inner try block throws an exception of type char*, which is caught by the inner handler. The inner handler rethrows the exception for the outer handler to catch.

try                             // outer try block.
 {
  try                           // inner (nested) try block.
   {
    throw "String thrown";      // throwing a string
   }
  catch(const char* caught)     // inner handler first catches exception.
   {
                                // do some processing.
    throw;                      // and rethrow the exception for outer handler.
   }
 }
catch(exception& caught)        // catch exceptions of type exception.
 {
                                // process exception
 }
catch(constant char* caught)    // catch exceptions of type constant char*.
 {
                                // process exception
 }
catch(...)                      // ellipsis (...) causes all exceptions to be caught.
 {
                                // process exception
 }