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 handle only synchronous exceptions.
The purpose of the exception handling mechanism is to provide means to detect and report an exceptional circumstance or condition, so that appropriate action can be taken against it. The mechanism suggests a separate error handling code that performs the following tasks:
- Find the problem (hit on the exception).
- Inform that an error has occurred (throw the exception).
- Receive the error information (catch the exception).
- Take corrective actions (handle the exception).
The error handling code basically consists of two segments; one to detect errors and to throw exceptions, and the other to catch the exceptions and to take appropriate action.
Exception Handling Mechanism
C++ exception handling mechanism is basically built upon three keywords, namely, try , throw, and catch . The keyword try is used to preface a block of statements (surrounded by braces) which may generate exceptions. This block of statements is known as try block. When an exception is detected, it is thrown using a throw statement in the try block. Catch blocks defined by the keyword catch, 'catches' the exception 'thrown' by the throw statement in the try block, and handles it appropriately.
The catch block that catches an exception must immediately follow the try block that throws the exception. The general form of these two blocks is as follows:
When a try block throws an exception, the program control leaves the try block and enters the catch statement of the catch block. Note that exceptions are objects used to transmit information about a problem. If the type of object thrown matches the argument- type in the catch statement, then catch block is executed for handling the exception. If it does not match, the program is aborted with the help of the abort() function which is invoked by default. When no exception is detected and thrown, the control goes to the statement immediately after the catch block, i.e., the catch block is skipped. This is shown in the program below:
The above program detects and catches a division-by-zero error. Different values of a and b will show how the exception mechanism works.
Re-throwing an Exception
A handler may decide to re-throw an exception caught without processing it. In such situations, we may simply invoke throw without any arguments as shown below:
This causes the current exception to be thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block. The following program demonstrates how an exception is re-thrown and caught.
Example: Program to illustrate re-throwing of exception
Output:
When an exception is re-thrown, it will not be caught by the same catch statement or any other catch in that group. Rather, it will be caught by an appropriate catch in the outer try/ catch sequence only.
A catch handler itself may detect and throw an exception. Here again, the exception thrown will not be caught by any catch statements in that group. It will be passed on to the next outer try/catch sequence for processing.
Exception Specification
It is possible to restrict a function to throw only certain specified exceptions. This is achieved by adding a throw list clause to the function definition. The general form of using an exception specification is:
The type-list specifies the type of exceptions that may be thrown by the function. Throwing any other type of exception will cause abnormal program termination. If we wish to prevent a function from throwing any exception, we may do so by making the type-list empty. That is, we must use,
Comments - No Responses to “Exception Handling”
Sorry but comments are closed at this time.