Exception Handling
Exception Handling
Exception are run time anomalies or unusual condition that a program may
encounter during execution.
Type of Program errors
We distinguish between the following types of errors:
• Syntax error: Errors due to the fact that the syntax of the language
is not respected.
• Semantic error: Errors due to an improper use of program
statements.
• Logical errors: Errors due to the fact that the specification is not
respected.
From the point of view of when errors are detected
Compile time error: Syntax errors & Static semantic errors indicated
by the compiler.
Runtime errors: Dynamic semantic errors, that errors, that cannot be
detected by the compiler(debugging).
Exception Handling
Runtime Errors
Division by zero
Access to an array outside of its bounds
Running out of memory
Running out of disk space
It was not a part of original C++
It is a new feature added to ANSI C++
Exception are Two kinds:
Synchronous Exception
Out of rage
Over flow
Asynchronous Exception
Error that are caused by causes beyond the control of the program.
Keyboard interrupts.
In C++ only Synchronous exception can be handled.
Exception Handling
Exception Handling Mechanism
Find the problem (Hit the Exception)
Inform that an error has occurred (Throw the Exception)
Receive the error information (Catch the Exception)
Take corrective action (Handle the Exception)
Exception in Java can be categorized into two types:
Checked Exception:
These Exceptions are explicitly handled in the code itself with the help of
try-catch blocks.
Checked Exceptions are extended from the java.lang.Exceptionclass
UnChecked Exception:
These Exception are not essentially handled in the program code; instead
the JVM handles such exceptions.
Unchecked Exceptions are extended from the
java.lang.RuntimeException class
Exception Handling Mechanism
It is basically build upon three keywords.
Try
Throw
Catch Try Block
Detects & throws and Exception
Throws
Exception
Object
Catch Block
Catch & handle the Exception
Exception Handling Mechanism
Invoke function
Throws Exception
Throwing Mechanism
The throw statement can have one of the following 3 forms
Throw new Throwable subclass;
Throw(exception)
Throw exception
Throw //used to re-throw a exception
• The operand object exception can be of any type, including
constant.
• It is also possible to throw an object 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
The type indicates the type of exception the catch block handles.
The parameter arg is an optional parameter name.
The catch statement catches an exception whose type matches with
the type of the catch argument.
If the parameter in the catch statement is name, then the parameter
can be used in the exception handling code.
If a catch statement does not match the exception it is skipped.
More than one catch statement can be associated with a try block.
Catch(type arg)
{
….
….
….
}
Catching Mechanism
Multiple catch statement
Try {
statement; //generates an exception
}
Catch(type 1 arg) {
statement; //catch block 1
}
Catch(type 2 arg) {
statement; //catch block 2
}
…
...
Catch(type N arg) {
statement; //catch block N
}
Catching Mechanism
When an exception is thrown, the exception handlers are
searched in order for a match.
The first handler that yields a match is executed.
If several catch statement matches the type of an
exception the first handler that matches the exception type
is executed.
Catch all exception
Catch(….)
{
//statement for processing all exception
}
Using Finally statement
Try {
Try-block }
Catch(Exception class 1 e) {
catch-block }
Catch(Exception class 2 e) {
catch-block }
…
Finally {
Finally-block }
Using Finally statement
Try-block: sequence of statements that will be executed
under the control of the following catch clauses.