Exception Handling
Exception Handling
Reviewed
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 mechanism suggests a separate error handling code that performs the
following tasks
Exception Handling 1
throw − A program throws an exception when a problem shows up. This is done
using a throw keyword.
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
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.
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.
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.
CATCHING MECHANISM
Code for handling exceptions is included in catch blocks.
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.
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
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.
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
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