Java 中方法重寫時異常處理的規則是什麼?


當重寫一個丟擲異常的父類方法時,需要遵循某些規則。

應該丟擲相同的異常或子型別異常

如果父類方法丟擲某些異常,則子類中的方法應該丟擲相同的異常或其子型別異常。

示例

在以下示例中,父類的 readFile() 方法丟擲 IOEXception,而子類的 readFile() 方法丟擲 FileNotFoundException 異常。

由於 FileNotFoundException 異常是 IOException 的子型別,因此該程式可以編譯並執行,沒有任何錯誤。

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.

示例

同樣,如果子類丟擲與父類相同的異常,則程式可以編譯併成功執行。

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

不應丟擲父型別異常

如果父類方法丟擲某些異常,則子類中的方法不應丟擲其父型別異常。

示例

在以下示例中,父類的 readFile() 方法丟擲 FileNotFoundException 異常,而子類的 readFile() 方法丟擲 IOException,它是 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 ......
   }
}

編譯時錯誤

編譯時,上述程式會給出以下輸出:

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

不丟擲任何異常

如果父類方法丟擲某些異常,則可以重寫它而不丟擲任何異常。

示例

在以下示例中,父類的 sampleMethod() 方法丟擲 FileNotFoundException 異常,而 sampleMethod() 方法根本不丟擲任何異常。儘管如此,該程式仍可以編譯並執行,沒有任何錯誤。

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

更新時間: 2019年10月10日

653 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告

© . All rights reserved.