2 - Exception - Handling (2) Ss
2 - Exception - Handling (2) Ss
HANDLING
Exception Handling
The two types of bugs are logic errors and syntactic errors.
Logic errors occur due to poor understanding of the problem and solution
procedure.
Syntactic errors arise due to poor understanding of the language itself.
Can detect these errors by using exhaustive debugging and testing procedures.
Some peculiar problems other than logic or syntax errors are known as
exceptions.
Exceptions are run time anomalies or unusual conditions that a program may
encounter while executing.
Ex: Division by zero, access to an array outside of its bounds, or running out of
memory or disk space.
When a program encounters an exceptional condition, it is important that it is
identified and dealt with effectively.
Types of Exceptions
Exception Handling
1. Synchronous exceptions: Ex: “out-of-range index” and “over-flow”
2. Asynchronous exceptions: The errors that are caused by events beyond the
control of the program like keyboard interrupts.
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” so that appropriate action can be taken.
The mechanism suggests a separate error handling code that performs the
following tasks: 1. Find the problem (Hit the exception).
2. Inform that an error has occurred (Throw the exception).
3. Receive the error information (Catch the exception).
4. Take corrective actions (Handle the exception).
The error handling code consists of two segments, detect errors & throw
exceptions, and catch the exceptions and take appropriate actions.
Exception Handling Mechanism
C++ exception handling mechanism is built upon
three keywords, namely, try, throw, and catch.
Try: 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.
A catch block 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.
Exception Handling Mechanism
Exception Handling Mechanism
Exception Handling Mechanism
Exceptions are thrown by functions that are invoked from within the try blocks .
The point at which the throw is executed is called the throw point.
Once an exception is thrown to the catch block, control cannot return to the
throw point.
Exception Handling Mechanism
Throwing Mechanism
When an exception that is desired to be handled is detected, it is thrown using the
throw statement:
The type indicates the type of exception that catch block handles.
The parameter arg is an optional parameter name.
The exception-handling code is placed between 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.
Catching Mechanism
If the parameter in the catch statement is named, 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 mismatch, if an exception is not caught, abnormal program termination
will occur.
The catch block is simply skipped if the catch statement does not catch an
exception.
Multiple Catch Statements
A program segment can have more than one condition to throw an exception.
In such cases, associate more than one catch statement with a try (like the conditions
in a switch statement).
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.
When the arguments of several catch statements match the
type of an exception, the first handler that matches the
exception type is executed.
Multiple Catch Statements
Multiple Catch Statements
Catch All Exceptions
When it is not be able to anticipate all possible types of exceptions and may not be
able to design independent catch handlers to catch them.
In such circumstances, it is possible to force a catch statement to catch all
exceptions instead of a certain type alone.
This could be achieved by defining the catch statement using ellipses as follows:
All the throws were caught by the catch(...) statement.
It may be a good idea to use the catch(…) as a default
statement along with other catch handlers so that it can
catch all those exceptions which are not handled explicitly.
catch(...) should always be placed last in the list of
handlers. Placing it before other catch blocks would prevent those blocks from
catching exceptions.
Multiple Catch Statements
Rethrowing an Exception
A handler may decide to rethrow the exception caught without processing it.
In such situations, invoke throw without any arguments: throw;
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.
When an exception is rethrown, it will not be caught by the same catch statement
or any other catch in that group.
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.
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.
Rethrowing an Exception
Specifying Exceptions
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.
Throwing any other type of exception will cause
abnormal program termination.
To prevent a function from throwing any exception, make the type-list empty. i.e.
use throw(); // Empty list in the function header line.
Specifying Exceptions
Specifying Exceptions
Exceptions in Constructors and Destructors
Implications of exceptions thrown during the execution of a constructor routine in
C++:
The object is not yet fully constructed; its destructor would not be called once the
program control goes out of the object’s context.
If the constructor had reserved some memory before the exception was raised, then
there would be no mechanism to prevent such memory leak.