Saturday, 23 May 2015

Exception handling and method overriding

There are certain restrictions while overriding a method in case of exception handling in Java. Broadly there are two rules -

  • If superclass method has not declared any exception using throws clause then subclass overridden method can't declare any checked exception though it can declare unchecked exception.
  • If superclass method has declared an exception using throws clause then subclass overridden method can do one of the three things.
    1. sub-class can declare the same exception as declared in the super-class method.
    2. subclass can declare the subtype exception of the exception declared in the superclass method. But subclass method can not declare any exception that is up in the hierarchy than the exception declared in the super class method.
    3. subclass method can choose not to declare any exception at all.

If superclass method has not declared any exception

If superclass method has not declared any exception using throws clause then subclass overridden method can't declare any checked exception though it can declare unchecked exception.

Example

Exception&method overriding

It can be noted here that parent class' displayMsg() method deosn't have any throws clause whereas overridden method in the subclass declares IOException in its throws clause which is a checked exception. That's why the compile time error.

If we change the throws clause in subclass method to any unchecked exception then it won't result in compiler error.

public void displayMsg() throws ArrayIndexOutOfBoundsException{
}

If superclass method has declared an exception

  1. If superclass method has declared an exception then sub class can declare the same exception as declared in the superclass method.
    class Parent{
        public void displayMsg() throws IOException{
            System.out.println("In Parent displayMsg()");
        }
    }
    public class ExceptionOverrideDemo extends Parent{
        public void displayMsg() throws IOException{  
            System.out.println("In ExceptionOverrideDemo displayMsg()"); 
        }  
    }  
    
  2. subclass can declare the subtype exception of the exception declared in the superclass method.
    class Parent{
     public void displayMsg() throws IOException{
      System.out.println("In Parent displayMsg()");
     }
    }
    public class ExceptionOverrideDemo extends Parent{
     public void displayMsg() throws FileNotFoundException{  
      System.out.println("In ExceptionOverrideDemo displayMsg()"); 
     }  
    
    }
    

    Here in super class displayMsg() method throws IOException where as in subclass overridden displayMsg() method throws FileNotFoundException. Since FileNotFoundException is the subtype (Child class) of IOException so no problem here.

  3. But subclass method can not declare any exception that is up in the hierarchy than the exception declared in the super class method.
    Exception & method overriding2
    Here parent class method is throwing IOException whereas in the subclass overridden method is throwing Exception, it will result in compiler error as IOException is the child class of Exception class, thus Exception is up in the hierarchy.
  4. Subclass overridden method declares no exception. Subclass overridden method can choose to not throw any exception at all even if super class method throws an exception.
    class Parent{
        public void displayMsg() throws IOException{
            System.out.println("In Parent displayMsg()");
        }
    }
    public class ExceptionOverrideDemo extends Parent{
        public void displayMsg(){  
            System.out.println("In ExceptionOverrideDemo displayMsg()"); 
        }  
    
    }
    

That's all for this topic Exception handling and method overriding. If you have any doubt or any suggestions to make please drop a comment. Thanks!



Related Topics

  1. Java Exception Handling interview questions
  2. Method overriding in Java
  3. throws clause
  4. throw statement
  5. Creating custom exception class
  6. Multi catch statement in Java 7

You may also like -

No comments:

Post a Comment