3.1.7 Exceptions Thrown by Handlers3.1.6 The finally Clause3.1 Exception Handling3.1.8 Runtime Exceptions

3.1.7 Exceptions Thrown by Handlers

An exception handler may itself throw an exception; either because an error has occurred in the handler itself or because the handler wants to further propagate a caught exception. An exception thrown by a handler is not handled by the handlers in the current try statement but propagated upwards.

The case of a handler first catching an exception and then propagating it further upwards is demonstrated by the following program:

  static void m1()
  {
    try
    {
      m2();
    }
    catch (MyException e)
    {
      ... // m1-specific handling of e
    }
  }

  static void m2() throws MyException
  {
    try
    {
      ...
    }
    catch (MyException e)
    {
       ... // m2-specific handling of e
       throw e;
    }
  }

The protected code block in m2 may throw an exception which is caught by a corresponding handler and, after appropriate handling, further propagated upwards where it is again caught by the handler in m1. This demonstrates that the throw clause needs not always create a new exception object; it may use any already existing object for this purpose.


© Wolfgang Schreiner; February 3, 2005

3.1.7 Exceptions Thrown by Handlers3.1.6 The finally Clause3.1 Exception Handling3.1.8 Runtime Exceptions