When overriding a superclass method, you need to follow certain rules if the method throws an exception.
If a superclass method throws a certain exception, the method in the subclass should throw the same exception or its subtype.
In the following example, the superclass's readFile() method throws an IOException, while the subclass's readFile() method throws a FileNotFoundException.
Since the FileNotFoundException exception is a subtype of IOException, the program can compile and execute 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"); } } }
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.
Similarly, if the subclass throws the same exception as the superclass, the program will compile and execute 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(); } }
Method of Subclass
If a superclass method throws an exception, the method in the subclass should not throw its superclass exception kind.
In the following example, the superclass's readFile() method throws a FileNotFoundException exception, while the subclass's readFile() method throws an IOException exception, which is a superclass of FileNotFoundException. kind.
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 ...... } }
While compiling, the above program gives 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
If the parent class method throws throw certain exceptions, you can override it without throwing any exceptions.
In the following example, the sampleMethod() method of the parent class throws a FileNotFoundException exception, while the sampleMethod() method of the subclass does not throw any exception at all. Nonetheless, the program compiles and executes 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(); } }
Method of Subclass
The above is the detailed content of What are the rules for method coverage and exception handling in Java?. For more information, please follow other related articles on the PHP Chinese website!