Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exceptions

Any runtime error generates an exception. Exceptions terminate the execution. It is possible to catch an exception instead of terminating the execution:

    auto e;
    try
    {
      ... some statements that cause a runtime error...
    }
    catch ( e )
    {
      // e holds the exception information
      // it is an instance of the exception class
    }

See the details of classes.

The try/catch blocks can be nested. If the current function has no try/catch blocks, the calling function will be examined, and so on, until we find a try/catch block or exit the main function. If no try/catch block is found, an unhandled exception is reported.

It is also possible to throw an exception explicitly. Any object can be thrown. For example:

    throw 5;

will throw value ‘5’.