What are the rules of exception handling with respect to method overriding in java?


While a superclass method throws an exception while overriding it you need to follow the certain rules.

Should throw Same exception or, sub type

If the super-class method throws certain exception, the method in the sub-class should throw the same exception or its sub type.

Example

In the following example, the readFile() method of the super-class throws an IOEXception and, the readFile() method of the sub-class throws FileNotFoundException exception.

Since the FileNotFoundException exception is the sub type of the IOException this program gets compiled and executed without any errors.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
abstract class Super {
   public String readFile(String path) throws IOException {
      throw new IOException();
   }
}
public class ExceptionsExample extends Super {
   @Override
   public String readFile(String path) throws FileNotFoundException {
      Scanner sc = new Scanner(new File("E://test//sample.txt"));
      String input;
      StringBuffer sb = new StringBuffer();
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(" "+input);
      }
      return sb.toString();
   }
   public static void main(String args[]) {
      String path = "E://test//sample.txt";
      ExceptionsExample obj = new ExceptionsExample();
      try {
         System.out.println(obj.readFile(path));
      }catch(FileNotFoundException e) {
         System.out.println("Make sure the specified file exists");
      }
   }
}

Output

Tutorials Point is an E-learning company that set out on its journey to provide knowledge to that class of readers that responds better to online content. With Tutorials Point, you can learn at your own pace, in your own space. After a successful journey of providing the best learning content at tutorialspoint.com, we created our subscription based premium product called Tutorix to provide Simply Easy Learning in the best personalized way for K-12 students, and aspirants of competitive exams like IIT/JEE and NEET.

Example

In the same way if the sub-class throws the same exception as the super-class the program gets compiled and executed successfully.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
abstract class Super {
   public void sampleMethod()throws FileNotFoundException {
      System.out.println("Method of superclass");
   }
}
public class ExceptionsExample extends Super {
   public void sampleMethod()throws FileNotFoundException {
      System.out.println("Method of Subclass");
   }
   public static void main(String args[]) {
      ExceptionsExample obj = new ExceptionsExample();
      obj.sampleMethod();
   }
}

Output

Method of Subclass

Should not throw an exception of super type

If the super-class method throws certain exception, the method in the sub-class should not throw its super type.

Example

In the following example the readFile() method of the super-class throws FileNotFoundException exception and, the readFile() method of the sub-class throws an IOException, which is the super type of the FileNotFoundException.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
abstract class Super {
   public String readFile(String path)throws FileNotFoundException {
      throw new FileNotFoundException();
   }
}
public class ExceptionsExample extends Super {
   @Override
   public String readFile(String path)throws IOException {
      //method body ......
   }
}

Compile time error

On compiling, the above program gives you the following output −

ExceptionsExample.java:13: error: readFile(String) in ExceptionsExample cannot override readFile(String) in Sup
   public String readFile(String path)throws IOException {
                 ^
   overridden method does not throw IOException
1 error

Without throwing any exception

If the super-class method throws certain exception, you can override it without throwing any exception.

Example

In the following example the sampleMethod() method of the super-class throws FileNotFoundException exception and, the sampleMethod() method does not throw any exception at all. Still this program gets compiled and executed without any errors.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
abstract class Super {
   public void sampleMethod()throws FileNotFoundException {
      System.out.println("Method of superclass");
   }
}
public class ExceptionsExample extends Super {
   public void sampleMethod() {
      System.out.println("Method of Subclass");
   }
   public static void main(String args[]) {
      ExceptionsExample obj = new ExceptionsExample();
      obj.sampleMethod();
   }
}

Output

Method of Subclass
raja
Published on 10-Oct-2019 10:13:12
Advertisements