Exception Handling with Method Overriding

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Exception Handling with Method Overriding in Java

There are many rules if we talk about methodoverriding with exception handling. The Rules are as follows:

  • If the superclass method does not declare an exception
    • If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
  • If the superclass method declares an exception
    • If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

If the super class method does not declare an exception

1) Rule: If the super class method does not declare an exception, subclass overridden method cannot declare the checked exception.

  1. import java.io.*;  
  2. class Parent{  
  3.   void msg(){System.out.println("parent");}  
  4. }  
  5.   
  6. class TestExceptionChild extends Parent{  
  7.   void msg()throws IOException{  
  8.     System.out.println("TestExceptionChild");  
  9.   }  
  10.   public static void main(String args[]){  
  11.    Parent p=new TestExceptionChild();  
  12.    p.msg();  
  13.   }  
  14. }  

Test it Now

Output:Compile Time Error

2) Rule: If the super class method does not declare an exception, subclass overridden method cannot declare the checked exception but can declare unchecked exception.

  1. import java.io.*;  
  2. class Parent{  
  3.   void msg(){System.out.println("parent");}  
  4. }  
  5.   
  6. class TestExceptionChild1 extends Parent{  
  7.   void msg()throws ArithmeticException{  
  8.     System.out.println("child");  
  9.   }  
  10.   public static void main(String args[]){  
  11.    Parent p=new TestExceptionChild1();  
  12.    p.msg();  
  13.   }  
  14. }  

Test it Now

Output:child

If the super class method declares an exception

1) Rule: If the super class method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

Example in case subclass overridden method declares parent exception

  1. import java.io.*;  
  2. class Parent{  
  3.   void msg()throws ArithmeticException{System.out.println("parent");}  
  4. }  
  5.   
  6. class TestExceptionChild2 extends Parent{  
  7.   void msg()throws Exception{System.out.println("child");}  
  8.   
  9.   public static void main(String args[]){  
  10.    Parent p=new TestExceptionChild2();  
  11.    try{  
  12.    p.msg();  
  13.    }catch(Exception e){}  
  14.   }  
  15. }  

Test it Now

Output:Compile Time Error

Example in case subclass overridden method declares same exception

  1. import java.io.*;  
  2. class Parent{  
  3.   void msg()throws Exception{System.out.println("parent");}  
  4. }  
  5.   
  6. class TestExceptionChild3 extends Parent{  
  7.   void msg()throws Exception{System.out.println("child");}  
  8.   
  9.   public static void main(String args[]){  
  10.    Parent p=new TestExceptionChild3();  
  11.    try{  
  12.    p.msg();  
  13.    }catch(Exception e){}  
  14.   }  
  15. }  

Test it Now

Output:child

Example in case subclass overridden method declares subclass exception

  1. import java.io.*;  
  2. class Parent{  
  3.   void msg()throws Exception{System.out.println("parent");}  
  4. }  
  5.   
  6. class TestExceptionChild4 extends Parent{  
  7.   void msg()throws ArithmeticException{System.out.println("child");}  
  8.   
  9.   public static void main(String args[]){  
  10.    Parent p=new TestExceptionChild4();  
  11.    try{  
  12.    p.msg();  
  13.    }catch(Exception e){}  
  14.   }  
  15. }  

Test it Now

Output:child

Example in case subclass overridden method declares no exception

  1. import java.io.*;  
  2. class Parent{  
  3.   void msg()throws Exception{System.out.println("parent");}  
  4. }  
  5.   
  6. class TestExceptionChild5 extends Parent{  
  7.   void msg(){System.out.println("child");}  
  8.   
  9.   public static void main(String args[]){  
  10.    Parent p=new TestExceptionChild5();  
  11.    try{  
  12.    p.msg();  
  13.    }catch(Exception e){}  
  14.   }  
  15. }  

Test it Now

Output:child