Exception Handling
Created @November 13, 2023 9:05 AM
Reviewed
BASICS OF EXCEPTION HANDLING
An exception is a problem that arises during the execution of a program.
Exceptions are of two kinds-
1. Synchronous exceptions : Errors such as “out-of-range index” and “over-flow”
belong to the synchronous type exceptions.
2. Asynchronous 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” 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).
EXCEPTION HANDLING MECHANISM
Exceptions provide a way to transfer control from one part of a program to another. C++
exception handling is built upon three keywords: try, catch, and throw.
Exception Handling 1
throw − A program throws an exception when a problem shows up. This is done
using a throw keyword.
catch − A program catches an exception with an exception handler at the place in a
program where you want to handle the problem. The catch keyword indicates the
catching of an exception.
try − A try block identifies a block of code for which particular exceptions will be
activated. It's followed by one or more catch blocks.
try
{.....throw exception; // Block of statements which
..... // detects and throws an exception
}
catch(type arg) // Catches exception
{.......... // Block of statements that
..... // handles the exception
}
When the try block throws an exception, the program control leaves the try block
and enters the catch statement of the catch block
Exceptions are objects used to transmit information about a problem.
If the type of object thrown matches the arg type in the catch statement, then catch
block is executed for handling the exception.
Exception Handling 2
If they do 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
Exceptions are also 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.
type function(arg // Function with exception
{
throw(object) // Throws exception
}
try
{
//Invoke function here.....
}
catch(type arg) // Catches exception
{
.....
..... Handles exception here.....
}
Note : The try block is immediately followed by the catch block, irrespective of the
location of the throw point.
THROWING MECHANISM
Exception Handling 3
When an exception is thrown using the throw statement in one of the following
forms:
throw(exception);
throw exception;
throw; // used for rethrowing an exception
The operand object exception may be of any data 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 and the control exits the current try block, and is transferred to the
catch block after that try block.
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.
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
CATCHING MECHANISM
Code for handling exceptions is included in catch blocks.
A catch block looks like a function definition and is of the form-
catch(type arg){
// Statements for
// managing exceptions
}
The type indicates the type of exception that catch block handles. The parameter
arg is an optional parameter name.
Exception Handling 4
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 statement is named, then the parameter can be used
in the exception-handling code.
The catch block is simply skipped if the catch statement does not catch an
exception.
Multiple Catch Statements
A program can have more than one catch statement with a try (much like the conditions
in a switch statement) as shown below:
try{
// try block}
catch(type1 arg)
{ // catch block1
}
catch(type2 arg){
// catch block2
}
..........
catch(typeN arg){
// catch blockN
}
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
It is possible that arguments of several catch statements match the type of an
exception. In such cases, the first handler that matches the exception type is
executed.
Catch All Exceptions
Catch statement to catch all exceptions instead of a certain type alone.
Exception Handling 5
catch(...)
{ // Statements for processing
// all exceptions
}
Note -
1. catch(...) should always be placed last in the list of handlers. Placing it before other
catch blocks would prevent those blocks from catching exceptions.
2. all the throws were caught by the catch(...) statement.
SPECIFYING EXCEPTIONS
To restrict a function to throw only certain specified exceptions. This is achieved by
adding a throw list clause to the function definition
type function(arg-list) throw (type-list)
{.......... Function body.....
}
type-list specifies the type of exceptions that may be thrown.
Throwing any other type of exception will cause abnormal program termination.
Prevent a function from throwing any exception, we may do so by making the type-
list empty- throw(); // Empty list
Exception Handling 6
Exception Handling 7
Exception Handling 8