
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
- Related Articles
- Exception handling with method overriding in Java.
- What are the rules on method overriding in Java?
- What are the rules need to follow when overriding a method that throws an exception in Java?
- Rules for Java method overriding
- Can the overriding method throw the super-type of the exception thrown by the overridden method in Java?
- Guidelines to follow in while overriding a method that throws an exception in java?
- Importance of Proper Exception Handling in Java
- method overriding with access modifiers in Java
- Exception Handling in C++ vs Java
- Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?
- java access modifiers with method overriding
- What are the golden rules for handling your money?
- What are the best practices for exception handling in Python?
- Comparison of Exception Handling in C++ and Java
- Method overriding in Java
