
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the rules of exception handling with respect to method overriding in java?
While a superclass method throws an exception when overriding it, you need to follow certain rules. We will be discussing these rules in this chapter.
What is Exception Handling in Java?
Exception handling is a mechanism to handle errors in the program. For example, if a program tries to divide a number by zero, it will throw an ArithmeticException. In such cases, the program will terminate abnormally. To avoid this, we can use exception handling.
Now, let's discuss the rules of exception handling with respect to method overriding. Those are:
- Should throw the same exception or a subtype
- Should not throw an exception of super type
- Without throwing any exception
Let's discuss each of these rules in detail.
Should throw Same exception or, subtype
If the super-class method throws a certain exception, the method in the sub-class should throw the same exception or its subtype.
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
On executing the above program, you will get the following output:
Tutorials Point is an E-learning company that set out on its journey to provide knowledge to that class of readers who respond 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.FileNotFoundException; 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
Following is the output of the above program:
Method of Subclass
Should not throw an exception of super type
If the super-class method throws a 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.FileNotFoundException; 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 FileNotFoundException { // method body ...... return "File content"; } }
Output
On compiling, the above program gives you the following output:
ExceptionsExample.java:13: error: readFile(String) in ExceptionsExample cannot override readFile(String) in Super 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 a 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.FileNotFoundException; abstract class Super { public void sampleMethod() throws FileNotFoundException { System.out.println("Method of superclass"); } } public class ExceptionsExample extends Super { // This is valid because the overridden method in the subclass can choose not to throw any exceptions, // even if the superclass method declares exceptions. public void sampleMethod() { System.out.println("Method of Subclass"); } public static void main(String args[]) { ExceptionsExample obj = new ExceptionsExample(); obj.sampleMethod(); } }
Output
Following is the output of the above program:
Method of Subclass