Java Tutorial : Implementing Exception Handling – Part 2
Handling the Unreachable Code Problem
The multiple catch blocks generate unreachable code error. If the first catch block contains the Exception class object then the subsequent catch blocks are never executed. The Exception class being the superclass of all the exception classes catches various types of exceptions. The Java compiler gives an error stating that the subsequent catch blocks have not been reached. This is known as the unreachable code problem. To avoid unreachable code error, the last catch block in multiple catch blocks must contain the Exception class object.
The following code is an example of the unreachable code problem:
class UnreachableCodeDemo
{
public static void main(String args[])
{
int num1=5, num2=0, num3;
try
{
num3=num1/num2;
}
catch(Exception e)
{
System.out.println(“Error”);
}
catch(ArithmeticException e)
{
System.out.println(“Division by zero.”);
//Unreachable catch statement.
}
}
}
In the preceding code, the Java compiler gives an error stating that the catch block handling ArithmeticException has not been reached. This is because the Exception class, being the base class for all exceptions, handles all the exceptions that are raised by the Java code in the very first catch block. The output of the preceding code is:
The unreachable code problem can be solved if the subclasses, such as ArithmeticException, ArrayIndexOutOfBounds, or NullPointerException in the multiple catch statements are written before the Exception superclass.
Using the finally Clause
When an exception is raised, the statements in the try block written after the statement in which the exception occurred are ignored. The finally block is used to process certain statements, no matter whether an exception is raised or not. The block of code attached with the finally clause is executed after a try-catch block has been executed. The following syntax shows how to declare the try-finally block:
try
{
// Block of code
}
finally
{
// Block of code that is always executed irrespective of an
exception being raised or not.
}
If there is a catch block associated with the try block, the finally clause is written after the catch block. The following syntax shows how to declare the try-catch-finally block:
try
{
// Block of code.
}
catch(execption1 obj1)
{
System.out.println(“Exception1 has been raised”);
}
catch(exception2 obj2)
{
System.out.println(“Exception2 has been raised”);
}
finally
{
// Block of code that is always executed irrespective of an
exception being raised or not.
}
The finally block executes whether or not an exception is raised. If an exception is thrown, the finally block executes even if no catch statement matches the exception. For example, a file has to be closed irrespective of whether an exception is raised or not. You can place the code to close the file in both the try and catch blocks.
To avoid duplication of code, you can place the code in the finally block. The following code snippet is used to write a try-catch block to declare methods to open and write to a file:
try
{
openFile();
writeFile(); // Might raise an exception.
}
catch(ExceptionName obj)
{
// Process the exception.
}
finally
{
closeFile();
}
In the preceding code, the finally clause is executed regardless of whether an exception is thrown or not. You can have only one finally block for an exceptionhandler. You can use the finally clause to execute the closeFile() method.



Leave a Reply