Home> Java> javaTutorial> body text

What are the rules for method coverage and exception handling in Java?

WBOY
Release: 2023-09-06 18:29:13
forward
609 people have browsed it

What are the rules for method coverage and exception handling in Java?

When overriding a superclass method, you need to follow certain rules if the method throws an exception.

Should throw the same exception or a subtype

If a superclass method throws a certain exception, the method in the subclass should throw the same exception or its subtype.

Example

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"); } } }
Copy after login

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.
Copy after login

Example

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(); } }
Copy after login

Output

Method of Subclass
Copy after login
Copy after login

Superclass exceptions should not be thrown

If a superclass method throws an exception, the method in the subclass should not throw its superclass exception kind.

Example

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 ...... } }
Copy after login

Compile time error

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
Copy after login

Does not throw any exception

If the parent class method throws throw certain exceptions, you can override it without throwing any exceptions.

Example

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(); } }
Copy after login

Output

Method of Subclass
Copy after login
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!