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

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

WBOY
WBOYforward
2023-09-06 18:29:13622browse

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

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

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

Output

Method of Subclass

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 ......
   }
}

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

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

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Jar files in JavaNext article:Jar files in Java