Chapter 6 - Exception Handling
Chapter 6 - Exception Handling
2) Unchecked Exception: The classes that extend RuntimeException are known as unchecked exceptions
e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked
exceptionsarenotchecked at compile-timeratherthey arechecked at runtime.
Page 2 of 8
Subclasses Description
- ArithmeticException Arithmetic error such division by zero
- ArrayIndexOutOfBoundsException Array index out of bounds
RuntimeException
To avoid such runtime errors we need to use the try… catch … finally block.
6.3.2. Using Exception
The syntax:
try{
//statements that may arise runtime errors
}
catch(ExceptionType1 ob){
//exception handler code for ExceptionType1
}
catch(ExceptionType2 ob){
//exception handler code for ExceptionType2
}
//you can as many catch blocks
finally{
//statements to be executed after the try block ends
}
The try block contains statements that may raise an exception. When the exception occurs, an Exception
object of the correct type is created and thrown to the catch block that has a matching argument with the
created object type. The statements after the erroneous statement are jumped and execution goes to the
catch block. The catch block receives the exception description using its object argument. Then
statements in the catch block are executed only when an exception occurs in the try block.
The final block will be executed whether exception happened or not. It is used to do some finalizing
tasks such as closing opened files etc. The final block is optional.
Page 4 of 8
The following program demonstrates how to use the try..catch...finally block.
class ExceptionExample3{ Here when the exception
public static void main(String args[]){ occurs an
int x = 10, y = 0; ArithmeticException object
try{ is created and thrown to the
System.out.println(x/y);
catch block that has a
System.out.println("This will not be executed");
}
matching argument. The
catch(ArithmeticException ex){ statements after the
System.out.println("Division by zero"); erroneous statement in the
} try block will not be
finally{ executed. The statements in
System.out.print("This block is executed "); the catch block will be
System.out.println("whether or not exception occurs"); executed and everything
} after the catch block will be
System.out.println("this will be displayed now"); executed.
}
}
The following program corrects the array index out of bounds error.
Page 5 of 8
One or more catch blocks can be used. The block with a matching exception type will be selected and
executed. The following program demonstrates how.
class ExceptionExample5{ Having more than one catch
public static void main(String args[]){ blocks is important when one
int x = 10, y = 0; or more type of exceptions
try{ will be raised by the
System.out.println(x/y); statements in the try block.
System.out.println("This will not be executed");
In this example only the
}
catch block with
catch(ArrayIndexOutOfBoundsException ex){
ArithmeticException will
System.out.println("Invalid Array Index");
receive the exception thrown
}
by the try block.
catch(StringIndexOutOfBoundsException ex){
System.out.println("Invalid String Index");
}
catch(ArithmeticException ex){
System.out.println("Division by zero");
}
finally{
System.out.print("This block is executed ");
System.out.println("whether or not exception occurs");
}
System.out.println("this will be displayed now");
}
}
Super classes can be used as arguments to the catch block. This is possible because a superclass
reference variable can refer to the subclass’ object.
class ExceptionExample6{
public static void main(String a[]){
int x = 10, y = 0;
try{
System.out.println(x/y);
}
catch(RuntimeException e){
if(e instanceof ArithmeticException)
Page 6 of 8
System.out.println("Division By Zero");
else
System.out.println("Runtime error");
}
}
}
But if subclasses are used together with superclasses, the catch blocks with superclass object arguments
must be at the end. Otherwise compiler error will happen.
Page 7 of 8
Page 8 of 8