在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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP