Home     Blog

Throw and Catch Mechanism

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:

exception handling throw and catch mechanism clip image002 Throw and Catch Mechanism

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.

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Daniel Memetic on

Leave a Reply

Related Posts

  • Exception Handling

    Exceptions are of two kinds, namely, synchronous exceptions and asynchronous exceptions. Errors such as "out-of-range index" and "over-flow" belong to the synchronous type exceptions. The errors that are caused by events beyond the control of the program (such as keyboard interrupts) are called asynchronous exceptions. The proposed exception handling mechanism in C++ is designed to [...]...


  • Conditional Operators and Switch Statements

    C++ has an operator that can be used as an alternative to an if-statement. This operator is called the conditional operator (?:). This operator can be used to replace an if-else statement of the following general form: if(expression) {      statements ;      -      - } else {      statements ;      -      - } The above form of [...]...


  • if Statement

    An if/else statement tests a particular condition, if the condition evaluates to true, a course-of-action is followed i.e. a statement or a set of statements is executed. Otherwise another set of instructions are executed (the else block). Such statements are called program control statements that control the flow of execution in a program. Normally, the [...]...


  • break and continue Statements

    break Statement The break statement is used to alter the flow of control. When a break statement is executed in a while loop, for loop, do-while loop or switch statement, it causes immediate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from [...]...


  • goto Statement

    In programming, significance has always been given to use of structured programming technique to build reliable software that are easy to debug, maintain and modify. In some cases, performance is more important than strict obedience to structured programming technique. In these cases, some unstructured programming technique may be used. For example, we can use break [...]...