ExceptionHandling with MethodOverriding 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 superclass method does not declare an exception

    Note
    1) Rule: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception.
    Example #1
    snippet
    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
    Output:Compile Time Error
    Note
    2) Rule: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but can declare unchecked exception.
    Example #2
    snippet
    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
    Output:child

    If the superclass method declares an exception

    Note
    1) Rule: If the superclass 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

    Example #3
    snippet
    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
    Output:Compile Time Error

    Example in case subclass overridden method declares same exception

    Example #4
    snippet
    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
    Output:child

    Example in case subclass overridden method declares subclass exception

    Example #5
    snippet
    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
    Output:child

    Example in case subclass overridden method declares no exception

    Example #6
    snippet
    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
    Output:child
  • Related Tutorial
    Follow Us
    https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
    Contents +