Exception Handling with MethodOverriding in Java

Java

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

If the super class method does not declare an exception

If the super class method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.

If the super class method declares an exception

If the super class 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

    import java.io.*;  
    class Parent{  
      void msg(){System.out.println("parent");}  
    }  
      
    class TestExceptionChild extends Parent{  
      void msg()throws IOException{  
        System.out.println("TestExceptionChild");  
      }  
      public static void main(String args[]){  
       Parent p=new TestExceptionChild();  
       p.msg();  
      }  
    }  
Output:Compile Time Error
    import java.io.*;  
    class Parent{  
      void msg(){System.out.println("parent");}  
    }  
      
    class TestExceptionChild1 extends Parent{  
      void msg()throws ArithmeticException{  
        System.out.println("child");  
      }  
      public static void main(String args[]){  
       Parent p=new TestExceptionChild1();  
       p.msg();  
      }  
    }  
Output:child

If the super class method declares an exception

Example in case subclass overridden method declares parent exception

import java.io.*;  
class Parent{  
  void msg()throws ArithmeticException{System.out.println("parent");}  
}  
  
class TestExceptionChild2 extends Parent{  
  void msg()throws Exception{System.out.println("child");}  
  
  public static void main(String args[]){  
   Parent p=new TestExceptionChild2();  
   try{  
   p.msg();  
   }catch(Exception e){}  
  }  
}
Output:Compile Time Error

Example in case subclass overridden method declares same exception

    import java.io.*;  
    class Parent{  
      void msg()throws Exception{System.out.println("parent");}  
    }  
      
    class TestExceptionChild3 extends Parent{  
      void msg()throws Exception{System.out.println("child");}  
      
      public static void main(String args[]){  
       Parent p=new TestExceptionChild3();  
       try{  
       p.msg();  
       }catch(Exception e){}  
      }  
    }  
Output:child

Example in case subclass overridden method declares subclass exception

import java.io.*;  
class Parent{  
  void msg()throws Exception{System.out.println("parent");}  
}  
  
class TestExceptionChild4 extends Parent{  
  void msg()throws ArithmeticException{System.out.println("child");}  
  
  public static void main(String args[]){  
   Parent p=new TestExceptionChild4();  
   try{  
   p.msg();  
   }catch(Exception e){}  
  }  
} 
Output:child

Example in case subclass overridden method declares no exception

    import java.io.*;  
    class Parent{  
      void msg()throws Exception{System.out.println("parent");}  
    }  
      
    class TestExceptionChild5 extends Parent{  
      void msg(){System.out.println("child");}  
      
      public static void main(String args[]){  
       Parent p=new TestExceptionChild5();  
       try{  
       p.msg();  
       }catch(Exception e){}  
      }  
    }  
Output:child
www.000webhost.com