All for Joomla All for Webmasters

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

1) Rule: If the superclass 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. class TestExceptionChild extends Parent{
  6.   void msg()throws IOException{
  7.     System.out.println(“TestExceptionChild”);
  8.   }
  9.   public static void main(String args[]){
  10.    Parent p=new TestExceptionChild();
  11.    p.msg();
  12.   }
  13. }
Output:Compile Time Error

2) Rule: If the superclass 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. class TestExceptionChild1 extends Parent{
  6.   void msg()throws ArithmeticException{
  7.     System.out.println(“child”);
  8.   }
  9.   public static void main(String args[]){
  10.    Parent p=new TestExceptionChild1();
  11.    p.msg();
  12.   }
  13. }
Output:child

If the superclass method declares an exception

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

  1. import java.io.*;
  2. class Parent{
  3.   void msg()throws ArithmeticException{System.out.println(“parent”);}
  4. }
  5. class TestExceptionChild2 extends Parent{
  6.   void msg()throws Exception{System.out.println(“child”);}
  7.   public static void main(String args[]){
  8.    Parent p=new TestExceptionChild2();
  9.    try{
  10.    p.msg();
  11.    }catch(Exception e){}
  12.   }
  13. }
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. class TestExceptionChild3 extends Parent{
  6.   void msg()throws Exception{System.out.println(“child”);}
  7.   public static void main(String args[]){
  8.    Parent p=new TestExceptionChild3();
  9.    try{
  10.    p.msg();
  11.    }catch(Exception e){}
  12.   }
  13. }
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. class TestExceptionChild4 extends Parent{
  6.   void msg()throws ArithmeticException{System.out.println(“child”);}
  7.   public static void main(String args[]){
  8.    Parent p=new TestExceptionChild4();
  9.    try{
  10.    p.msg();
  11.    }catch(Exception e){}
  12.   }
  13. }
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. class TestExceptionChild5 extends Parent{
  6.   void msg(){System.out.println(“child”);}
  7.   public static void main(String args[]){
  8.    Parent p=new TestExceptionChild5();
  9.    try{
  10.    p.msg();
  11.    }catch(Exception e){}
  12.   }
  13. }
    Output:child
PinIt
submit to reddit

Leave a Reply

Top