Detecting and Recovering from Errors


To detect and recover from errors in Standards<ToolKit>, you must use C++ try...catch blocks in your code. Failure to catch an exception results in a call to terminate() function. The call to terminate() forces your application to abort and is unrecoverable. Usually it is better to provide your own exception handler that will safely shut down your application in case an unexpected error condition is encountered.

The following code fragment illustrates several techniques for catching an invalid index into a string.


 
void
foo( string& text )
  {
  try
    {
    char x = text[ 1000 ]; // 1000 is beyond the length of "text".

    // exception was thrown - this code never executes.
    cout << "character[ 1000 ] = " << x << endl;
    }
  catch ( out_of_range& error )
    {
    // This will catch the specific exception type "out_of_range".
    cout << "Caught error: " << error.what() << endl;
    }
  catch ( exception& error )
    {
    // This will catch all exceptions derived from the
    // ANSI/ISO exception base class.
    cout << "Caught error: " << error.what() << endl;
    }
  catch ( ... )
   {
   // This catch block will handle any thrown exception, although
   // you are not able to query any information about it. This is
   // often used as a "last resort" to keep your application from
   // crashing abnormally.
   }
}

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.