[英]Why I m getting Unreported Exception
請問為什么第13行中的錯誤是未報告的異常,必須在聲明聲明為pr的情況下將其捕獲
class Demo {
public static void main(String args[]) {
try {
int x = 43 / 0;
} catch (ArithmeticException ob) {
throw ob;
}
try {
int x = 43 / 0;
} catch (Exception ob) {
throw ob;
}
Exception ob = new Exception();
throw ob;
// Line 13 unreported exception Exception; must be caught or declared to be thrown
}
}
在代碼的最后一行,您將引發異常,但沒有任何處理方法。 您必須執行以下兩項之一:
try/catch
塊將其包圍 throws
關鍵字。 看到這樣的問題: Java中的異常的throws關鍵字 另外還有一個問題: 為什么在調用函數時必須“拋出異常”?
其次,添加代碼后,代碼將編譯,但執行后仍會引發異常:
線程“主”中的異常java.lang.ArithmeticException:/零
原因是在catch
塊中您正在重新引發異常。
您需要向上述拋出異常的方法以及所有調用該方法的方法添加throws
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.