在Java的catch塊中,`throw e`和`throw new Exception(e)`有什麼區別?
異常是在程式執行期間發生的執行時錯誤。以下是一些示例場景:
- 如果你有一個大小為10的陣列,如果你的程式碼中的一行試圖訪問該陣列的第11個元素。
- 如果你試圖將一個數字除以0(結果為無窮大,JVM不知道如何計算它)。
當發生異常時,程式會在導致異常的那一行突然終止,程式的其餘部分不會執行。為了防止這種情況,你需要處理異常。
Java中有兩種型別的異常。
- 未檢查異常 - 未檢查異常是在執行時發生的異常。這些也稱為執行時異常。這些包括程式設計錯誤,例如邏輯錯誤或API的錯誤使用。執行時異常在編譯時會被忽略。
- 已檢查異常 - 已檢查異常是在編譯時發生的異常,這些也稱為編譯時異常。這些異常在編譯時不能簡單地忽略;程式設計師應該注意(處理)這些異常。
異常處理
為了處理異常,Java 提供了 try-catch 塊機制。
try/catch 塊放置在可能生成異常的程式碼周圍。try/catch 塊中的程式碼被稱為受保護程式碼。
語法
try {
// Protected code
} catch (ExceptionName e1) {
// Catch block
}當 try 塊中引發異常時,JVM 不會終止程式,而是將異常詳細資訊儲存在異常堆疊中,然後繼續執行 catch 塊。
catch 語句涉及宣告你試圖捕獲的異常型別。如果 try 塊中發生異常,則會驗證 try 塊後面的 catch 塊(或塊)。
如果發生的異常型別在 catch 塊中列出,則異常將傳遞給 catch 塊,就像引數傳遞給方法引數一樣。
示例
import java.io.File;
import java.io.FileInputStream;
public class Test {
public static void main(String args[]){
System.out.println("Hello");
try{
File file =new File("my_file");
FileInputStream fis = new FileInputStream(file);
}catch(Exception e){
System.out.println("Given file path is not found");
}
}
}輸出
Given file path is not found
重新丟擲異常
當異常在 catch 塊中被捕獲時,你可以使用 throw 關鍵字(用於丟擲異常物件)重新丟擲它。
重新丟擲異常時,你可以直接丟擲相同的異常,而無需調整它,例如:
try {
int result = (arr[a])/(arr[b]);
System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}catch(ArithmeticException e) {
throw e;
}或者,將其包裝在一個新的異常中並丟擲它。當你將捕獲的異常包裝在另一個異常中並丟擲它時,這被稱為異常鏈或異常包裝,透過這樣做,你可以調整你的異常,丟擲更高級別的異常並保持抽象。
try {
int result = (arr[a])/(arr[b]);
System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}catch(ArrayIndexOutOfBoundsException e) {
throw new IndexOutOfBoundsException();
}示例
在下面的 Java 示例中,我們的 demoMethod() 中的程式碼可能會丟擲 ArrayIndexOutOfBoundsException 和 ArithmeticException。我們正在兩個不同的 catch 塊中捕獲這兩個異常。
在 catch 塊中,我們重新丟擲這兩個異常,一個是用更高的異常包裝,另一個是直接丟擲。
import java.util.Arrays;
import java.util.Scanner;
public class RethrowExample {
public void demoMethod() {
Scanner sc = new Scanner(System.in);
int[] arr = {10, 20, 30, 2, 0, 8};
System.out.println("Array: "+Arrays.toString(arr));
System.out.println("Choose numerator and denominator(not 0) from this array (enter positions 0 to 5)");
int a = sc.nextInt();
int b = sc.nextInt();
try {
int result = (arr[a])/(arr[b]);
System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}catch(ArrayIndexOutOfBoundsException e) {
throw new IndexOutOfBoundsException();
}catch(ArithmeticException e) {
throw e;
}
}
public static void main(String [] args) {
new RethrowExample().demoMethod();
}
}輸出
Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator(not 0) from this array (enter positions 0 to 5) 0 4 Exception in thread "main" java.lang.ArithmeticException: / by zero at myPackage.RethrowExample.demoMethod(RethrowExample.java:16) at myPackage.RethrowExample.main(RethrowExample.java:25)
輸出
Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator(not 0) from this array (enter positions 0 to 5) 124 5 Exception in thread "main" java.lang.IndexOutOfBoundsException at myPackage.RethrowExample.demoMethod(RethrowExample.java:17) at myPackage.RethrowExample.main(RethrowExample.java:23)
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP