Computer >> Máy Tính >  >> Lập trình >> Java

Các quy tắc xử lý ngoại lệ liên quan đến ghi đè phương thức trong java là gì?

Trong khi một phương thức siêu lớp ném ra một ngoại lệ trong khi ghi đè nó, bạn cần tuân theo các quy tắc nhất định.

Nên ném cùng một ngoại lệ hoặc, loại phụ

Nếu phương thức siêu lớp ném ra một ngoại lệ nào đó, thì phương thức trong lớp con sẽ ném cùng một ngoại lệ hoặc kiểu con của nó.

Ví dụ

Trong ví dụ sau, phương thức readFile () của lớp siêu ném ra IOEXception và phương thức readFile () của lớp con ném ra ngoại lệ FileNotFoundException.

Vì ngoại lệ FileNotFoundException là kiểu con của IOException nên chương trình này được biên dịch và thực thi mà không có bất kỳ lỗi nào.

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

Đầu ra

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.

Ví dụ

Theo cách tương tự nếu lớp con ném cùng một ngoại lệ với lớp siêu, chương trình sẽ được biên dịch và thực thi thành công.

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

Đầu ra

Method of Subclass

Không nên ném một ngoại lệ của loại super

Nếu phương thức siêu lớp ném ra một ngoại lệ nào đó, phương thức trong lớp con sẽ không ném kiểu siêu của nó.

Ví dụ

Trong ví dụ sau, phương thức readFile () của lớp siêu ném ra ngoại lệ FileNotFoundException và phương thức readFile () của lớp con ném ra một IOException, là kiểu siêu của 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 ......
   }
}

Lỗi thời gian biên dịch

Khi biên dịch, chương trình trên cung cấp cho bạn kết quả sau -

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

Không đưa ra bất kỳ ngoại lệ nào

Nếu phương thức siêu lớp ném ra một ngoại lệ nào đó, bạn có thể ghi đè nó mà không ném bất kỳ ngoại lệ nào.

Ví dụ

Trong ví dụ sau, phương thức sampleMethod () của siêu lớp ném ra ngoại lệ FileNotFoundException và, phương thức sampleMethod () hoàn toàn không ném ra bất kỳ ngoại lệ nào. Vẫn chương trình này được biên dịch và thực thi mà không có bất kỳ lỗi nào.

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

Đầu ra

Method of Subclass