Exception Handling With Method Overriding in Java

In the sub class you can override method of the super class and provide it a different implementation. But, what if that method in super class declares exceptions using throws clause. Does the overridden method in sub class inherit those exception declarations too, can you override those exceptions in the overridden method. Let’s try to find answer to these questions in this post exception handling with method overriding in Java.

Rules followed in case of exception handling with method overriding

There are certain rules laid out for exception handling in Java while overriding a method which are as follows-

  1. If the method in super class has not declared any exception then the overridden method in the sub class can’t declare any checked exception though it can declare unchecked exceptions.
  2. If the method in super class has some exceptions declared using throws clause then you have three options in the sub-class overridden method.
    1. Subclass method can declare the same exception as the parent class method.
    2. Subclass method can declare any child exception of the exception declared in the parent class method. But it can’t declare any exception higher in the hierarchy.
      As example– If parent class method declares IOException then child class method can declare FileNotFoundException because FileNotFoundException is the sub class of IOException. But trying to declare Exception with the child class method will result in error as Exception class is the parent class of IOException.
    3. Overridden child class method doesn’t declare any exception at all.

Exception handling with method overriding example

Let’s see some examples of the scenarios outlined above to make it clearer.

If the method in super class has not declared any exception

Here we have a class Shape which is a parent class and it has a method area(). There is one child class Circle where area() method is overridden. If area() method in the child class Circle declares IllegalArgumentException using throws clause then there is no error as IllegalArgumentException is an unchecked exception.

If you change the declared exception to ParseException then you will get a compiler error as ParseException is a checked exception.

Exception handling with method overriding

If the method in super class declares an exception

Now let’s see the possibilities when super class method declares exceptions.

1- Sub class overridden method can declare an exception which is the sub type of the super class method.

In the above code in parent class Read there is a method readFile() which declares IOException. You are overriding this method in the sub class FileRead and there it declares FileNotFoundException. That is ok as FileNotFoundException is the child class of IOException.

In the subclass method if you change the exception thrown to Exception then you will get an error as Exception is super type of IOException.

Output

2- Sub class overridden method throws no exception at all.

Here readFile() method in the sub class doesn’t declare any exception, though the super class method does.

3- Sub class method throws the same exception as the super class method.

That’s all for the topic Exception Handling With Method Overriding in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

One Comment

  1. Great post. I was checking constantly this blog and I’m impressed!

    Very useful information specifically the last part 🙂 I
    care for such info a lot. I was seeking this certain information for a long time.
    Thank you and good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.