
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are unreachable catch blocks in Java?
A block of statements that the program control can never reach under any circumstances can be called an unreachable block. Java does not support unreachable blocks, such code will cause a compile-time error.
Any code that is written but never executed is considered unnecessary by the Java compiler. In this article, we will discuss unreachable catch blocks in Java. But, before that, let's understand what is a catch-block in Java.
The catch Block in Java
The catch block is written just after the try block and cannot have any code between them. It is an exception handler and contains code that runs when an exception occurs.
Syntax
Following is the syntax of catch blocks:
try { // statements } catch(typeOfException e) { // statements } catch(typeOfException e) { // statements }
When a catch Block is Reachable?
A catch block is considered reachable by the compiler if both of the following conditions are true:
- A checked exception that is thrown in the try block is assignable to the parameter of the catch block.
- There is no previous catch clause whose parameter type is equal to or a supertype of the parameter type of the catch block.
Example
In the given example, the first catch block is reachable because NumberFormatException is exactly the type of exception thrown in the try block.
The second catch block is also reachable because Exception is a superclass of NumberFormatException, and it appears after the specific exception catch block.
public class ExampleCatchBlock { public static void main(String[] args) { try { //This statement throws NumberFormatException int i = Integer.parseInt("abc"); } catch(NumberFormatException nfe) { System.out.println("This block handles NumberFormatException"); } catch(Exception e) { System.out.println("This block handles all exception types"); } } }
On running the above Java program, you will get the following result:
This block handles NumberFormatException
Unreachable catch Block in Java
When we are using multiple catch blocks, the order of catch blocks must be from the most specific to the most general ones. To understand this line, you must be familiar with the fact that the catch block that references the Exception class must always be the last catch block because Exception is the superclass of all exceptions.
Therefore, subclasses of Exception must come first and superclasses later. If we keep superclasses first and subclasses later, the compiler will throw an unreachable catch block error.
Example
In the following Java program, NumberFormatException is unreachable because the Exception will catch the thrown exception before it.
public class UnreachableBlock { public static void main(String[] args) { try { // This statement throws NumberFormatException int i = Integer.parseInt("abc"); } catch(Exception e) { // This block handles all exception types System.out.println("This block handles all exception types"); } catch(NumberFormatException nfe) { // Unreachable System.out.println("This block handles NumberFormatException"); } } }
When you run the above code, the compiler will show the given error:
UnreachableBlock.java:9: error: exception NumberFormatException has already been caught } catch(NumberFormatException nfe) { ^ 1 error