June 27, 2022

Exception Handling With Method Overriding in Java

In a child class you can override methods of the super class and provide 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.

class Shape {
  public void area(int side){
    System.out.println("Calculate area");
  }
}
public class Circle extends Shape {
  // Overridden method
  public void area(int radius) throws IllegalArgumentException{
    System.out.println("Calculate area");
    double area = Math.PI * Math.pow(radius, 2);
    System.out.println("Circle area " + area);
  }
  public static void main(String[] args) {
    Shape shape;
    shape = new Circle();
    shape.area(5);		
  }
}

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.

class Read {
  public void readFile() throws IOException{
    System.out.println("read file");
  }
}
public class FileRead extends Read {
  // Overridden method
  public void readFile() throws FileNotFoundException{
    File file = new File("D://test.txt");
    BufferedReader br = new BufferedReader(new FileReader(file));
  }
  public static void main(String[] args) {
    FileRead fr = new FileRead();
    try {
      fr.readFile();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

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.

public void readFile() throws Exception{
  File file = new File("D://test.txt");
  BufferedReader br = new BufferedReader(new FileReader(file));
}
Output
Exception Exception is not compatible with throws clause in Read.readFile()
FileRead.java
2- Sub class overridden method throws no exception at all which is also OK.
class Read {
  public void readFile() throws IOException{
    System.out.println("read file");
  }
}
public class FileRead extends Read {
  // Overridden method
  public void readFile(){
    File file = new File("D://test.txt");
      try {
      BufferedReader br = new BufferedReader(new FileReader(file));
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  public static void main(String[] args) {
    FileRead fr = new FileRead();
    fr.readFile(); 
  }
}

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.

class Read {
  public void readFile() throws IOException{
    System.out.println("read file");
  }
}
public class FileRead extends Read {
  // Overridden method
  public void readFile() throws IOException{
    File file = new File("D://test.txt");
    BufferedReader br = new BufferedReader(new FileReader(file));
  }
  public static void main(String[] args) {
    FileRead fr = new FileRead();
    try {
      fr.readFile();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }	
  }
}

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

No comments:

Post a Comment