You are here: Home / Topics / Order of exception subclass and superclass in Java

Order of exception subclass and superclass in Java

Filed under: Java on 2023-09-19 06:47:05

//Program to explain that, when we use multiple catch statements, it is 
//important to remember that exception subclasses must come before any of their superclasses.

class  MultipleException
{
public static void main(String args[ ]) 
{
 try 
 {
  int a = 0;
  int b = 10 / a;
 } 
 catch(Exception e) 
 {
      System.out.println("Generic Exception catch.");
 }
 catch(ArithmeticException e) 
 {  //   ERROR – unreachable code
  System.out.println("This is never reached.");
 }
}
}


Output:

MultipleException.java|34| error| exception ArithmeticException has already  caugh
              catch(ArithmeticException e)
error

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.