You are on page 1of 8

TRY/CATCH/FINALLY

BLOCK
Try/Catch/Finally Block
Statements that might generate an exception are placed in
a try block. The try block also contain statements that
should not be executed if an exception occurs. The try
block followed by zero or more catch blocks. A catch
block specifies the types of exception it can catch and
contains an exception handler. The last catch block may
or may not be followed by a finally block. Any code
contained in a finally block always executes, regardless
whether an exception occurs, except when the program
exits early from a try block by calling the method
System.exit. If a try block has no catch block, then it
must have the finally block.
Exception Occurs
General syntax of the try/catch/finally block:
try{
//Statements
}catch(ExceptionClassName1 obj1){
//exception handler code
}catch(ExceptionClassName2 obj2){ A try block contains code
for normal circumstances,
//exception handler code
while catch block contains
}catch(ExceptionClassName3 obj3){ code to handle an
//exception handler code exception
}finally{
//statements
}
Rules: Try/Catch/Finally Block
 If no exception is thrown in a try block, all catch blocks
associated with the try block are ignored and program
execution resumes after the last catch block
 If an exception is thrown in a try block, the remaining
statements in the try block are ignored. The program
searches the catch blocks in the order in which they appear
after the try block and looks for an appropriate exception
handler.
 If the type of the thrown exception matches the parameter
type in one of the catch blocks, the code of that catch block
executes and the remaining catch blocks after this catch block
is ignored.
 If there is a finally block after the last catch block, the finally
block executes regardless of whether an exception occurs.
Rules: Try/Catch/Finally Block
As noted, when an exception occurs, an
object of a particular class type is created.
Example:
catch(ArithmeticException aeRef){
//exception handler code
}

*aeRef is the object for the class


ArithmeticException.
Rules: Catch Block
A catch block can catch either all exceptions of a
specific type or all types of exception. The heading
of a catch block specifies the type of exception it
handles.
try{

GENERAL
}catch(Exception eRef){ EXCEPTION

TO
}catch(ArithemeticException aeRef){
SPECIFIC
EXCEPTION
}
Example: Try/Catch Block
int divident, divisor, quotient;
try{
System.out.println(“Enter the divident”);
dividend = scan.nextInt();
System.out.println(“Enter the divisor”);
divisor = scan.nextInt();
quotient = divident / divisor;
System.out.println(“Quotient = ”+ quotient);
}catch(ArithmeticException aeRef){
System.out.println(“Exception: ”+ aeRef.toString());
}catch(InputMismatchException imeRef){
System.out.println(“Exception: ”+ imeRef.toString());
}
Java Exception Hierarchy
Every class you design can potentially cause
exceptions. Java provides extensive support
for exception handling by providing a number
of exception class. Java allows users to
create and implement their own exception
classes to handle exceptions not covered by
Java’s exception classes.

You might also like