Throw Mechanism

When an exception, which is desired to be handled, is detected, it is thrown using the throw statement. It can be used in any one of the following form:

  • throw(exception) ;
  • throw exception ;
  • throw ; // used for re-throwing an exception

The object passed to the throw statement may be of any type, including constants. It is also possible to throw objects not intended for error handling. When an exception is thrown, it will be caught by the catch statement associated with the try block, i.e., the control exits the current try block, and is transferred to the catch block. The throw point can be in a deeply nested scope within a try block or in a deeply nested function call. In any case, control is transferred to the catch statement.

Catch Mechanism

As stated in the previous tutorial, the code for handling exceptions is included in a catch block. A catch block looks like a function definition and is of the following form:

The type indicates the type of exception that a catch block handles. The parameter argument is the parameter’s name. Note that, the exception-handling code is placed between the two braces. The catch statement catches an exception whose type matches with the type of catch argument. When it is caught, the code in the catch block is executed.

If the parameter in the catch definition is given a name, then the parameter can be used in the exception handling code. After executing the handler, the control goes to the statement immediately following the catch block.

Due to a mismatch, if an exception is not caught, an abnormal program termination may occur. It is important to note that the catch block is simply skipped if the catch statement does not catch the exception.

Multiple Catch Statements

Sometimes, it happens that a program segment has more than one condition to throw an exception. In such a situation, we can associate more than one catch statement with a try, as shown below :

When an exception is thrown, the exception handlers are searched in order for an appropriate match. The first handler that yields a match is executed. After executing the handler, the control goes to the first statement after the last catch block for that try (i.e., all other handlers are bypassed). When no match is found, the program is terminated.

Example : Program to illustrate multiple catch statements execution

Output:

The program when executed first, invokes the function test() with x = 1 and therefore throws x as an int exception. This matches the type of the parameter m in catch2 and therefore ctch2 handler is executed. Immediately after the execution, the function test() is again invoked with x = 0. This time, the function throws ‘x’, a character type exception and therefore the first handler is executed. Finally, the handler catch3 is executed when a double type exception is thrown. Note that every time only the handler which catches the exception is executed and all other handlers are bypassed.

When the try block does not throw any exception and when it completes normal execution, the control passes to the first statement after the last catch handler associated with that try bock.