- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Guidelines to follow in while overriding a method that throws an exception 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.
In the same way if the sub-class throws the same exception as the super-class the program gets compiled and executed successfully.
Example
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 −
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 rowing any exception.
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.
Example
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
- Related Articles
- What are the rules need to follow when overriding a method that throws an exception in Java?
- Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?
- While overriding can the subclass choose not to throw an exception in java?
- Exception handling with method overriding in Java.
- Is parent child hierarchy important on throws while overriding in Java?
- Guidelines to be followed while implementing equals method in Java?
- If a method in parent class “throws Exception”, can we remove it in overridden method in java?
- How do you test that a Python function throws an exception?
- Is it possible to throw exception without using "throws Exception" in java?
- What are the rules of exception handling with respect to method overriding in java?
- Method overriding in Java
- Using “TYPE = InnoDB” in MySQL throws an exception?
- Can the overriding method throw the super-type of the exception thrown by the overridden method in Java?
- overriding method different package in java
- When Method Overriding occurs in Java?
