令人頭疼的 Java 異常知識點總結

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. 異常簡介","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.1 異常類層次結構","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3d/3d77c97d7b9922b467e0a04a62481862.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從結構圖可以看出,所有異常均繼承自 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Throwable","attrs":{}}],"attrs":{}},{"type":"text","text":" 類,它有兩個重要的子類:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Exception","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Error","attrs":{}}],"attrs":{}},{"type":"text","text":" ,各自又包含大量子類。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Exception","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"程序本身可以處理的異常","attrs":{}},{"type":"text","text":",又可以分爲 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"受檢異常","attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"非受檢異常","attrs":{}},{"type":"text","text":" ,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"受檢異常","attrs":{}},{"type":"text","text":" 可以用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"try...catch...","attrs":{}}],"attrs":{}},{"type":"text","text":" 語句進行捕獲處理,而且能從異常中恢復。但 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"非受檢異常","attrs":{}},{"type":"text","text":" 是程序運行時錯誤,會導致程序崩潰而無法恢復。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"受檢異常","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"編譯器要求必須處理的異常,正確的程序在運行時,經常會出現、預期範圍內的情況。一旦發生該類異常,就必須使用某種方式進行處理。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"包括除開 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"RuntimeException","attrs":{}}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 及其子類之外的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Exception","attrs":{}}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 異常","attrs":{}},{"type":"text","text":"。編譯器會檢查此類異常,所以我們必須使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"throws","attrs":{}}],"attrs":{}},{"type":"text","text":" 進行拋出或者 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"try...catch","attrs":{}}],"attrs":{}},{"type":"text","text":" 進行捕獲,否則將導致編譯失敗。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"非受檢異常","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"編譯器不會檢查而且也不要求我們進行處理,即就算在程序中出現了此類異常,即便我們沒有用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"try...catch","attrs":{}}],"attrs":{}},{"type":"text","text":" 進行捕獲或者用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"throws","attrs":{}}],"attrs":{}},{"type":"text","text":" 進行拋出,編譯都會成功。包括 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"RuntimeException","attrs":{}}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 及其子類和錯誤 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Error","attrs":{}}],"attrs":{}},{"type":"text","text":".","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同時也可以分爲:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"運行時異常和編譯時異常","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"運行時異常","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"RuntimeException","attrs":{}}],"attrs":{}},{"type":"text","text":" 類及其子類,表示 JVM 在運行期間可能出現的異常,Java 編譯器不會檢查它。沒有通過 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"throws","attrs":{}}],"attrs":{}},{"type":"text","text":" 拋出或 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"try...catch","attrs":{}}],"attrs":{}},{"type":"text","text":" 捕獲,仍然可以編譯通過,常見的有 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"NullPointerException、ArrayIndexOutBoundException、ClassCastException、ArithmeticException、NumberFormatException、IllegalArgumentException","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"編譯時異常","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Exception","attrs":{}}],"attrs":{}},{"type":"text","text":" 中除開運行時異常之外的異常,Java 編譯器會檢查它,一旦出現,必須使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"throws","attrs":{}}],"attrs":{}},{"type":"text","text":" 進行聲明拋出,或者使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"try...catch","attrs":{}}],"attrs":{}},{"type":"text","text":" 進行捕獲異常,否則不能通過編譯。常見的有 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ClassNotFoundException、IOException","attrs":{}}],"attrs":{}},{"type":"text","text":"。在程序中,通常不會自定義該類異常,而是直接用系統提供的異常類,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"該異常必須手動在代碼中添加捕獲語句來處理。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":2,"normalizeStart":2},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Error","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"程序無法處理的錯誤","attrs":{}},{"type":"text","text":",表示程序運行過程中教嚴重的問題,大多與 coder 所做操作無關,而是代碼運行時 JVM 出現的問題。此時說明故障發生於虛擬機本身、或者發生在虛擬機試圖執行應用時。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.2 Throwable 常用方法","attrs":{}}]},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
方法說明
public String getMessage()返回異常發生時的簡要描述
public String toString()返回異常發生時的詳細信息
public String getLocalizeMessage()返回異常對象的本地化信息,若子類重寫該方法,可以生成本地化信息,若未重寫,則返回信息同 getMessage() 方法
public void printStackTrace()在控制檯中打印異常對象封裝的異常信息
"}}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.3 try-catch-finally 和 try-with-resources","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"try-catch-finally","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"try","attrs":{}},{"type":"text","text":" :用於捕獲異常,後接零個或多個 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"catch","attrs":{}}],"attrs":{}},{"type":"text","text":",沒有 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"catch","attrs":{}}],"attrs":{}},{"type":"text","text":" 則必須加上 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"finally","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"catch","attrs":{}},{"type":"text","text":":用於處理 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"try","attrs":{}}],"attrs":{}},{"type":"text","text":" 捕獲到的異常;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"finally","attrs":{}},{"type":"text","text":":無論是否捕獲/處理異常,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"finally","attrs":{}}],"attrs":{}},{"type":"text","text":" 塊中內容均會執行,就算 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"try","attrs":{}}],"attrs":{}},{"type":"text","text":" 或 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"catch","attrs":{}}],"attrs":{}},{"type":"text","text":" 中有 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"return","attrs":{}}],"attrs":{}},{"type":"text","text":" 語句,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"finally","attrs":{}}],"attrs":{}},{"type":"text","text":" 中代碼也將在方法返回之前執行;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"try-with-resources","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當我們有必須要關閉的資源時,建議優先使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"try-with-resources","attrs":{}}],"attrs":{}},{"type":"text","text":",這樣寫出的代碼更加簡短清晰。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":3,"normalizeStart":3},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"兩者對比","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"// try-catch-finally\nScanner scanner = null;\ntry {\n scanner = new Scanner(new File(\"D:/demo.txt\"));\n while (scanner.hasNext()) {\n System.out.println(scanner.nextLine());\n }\n} catch (FileNotFoundException e) {\n e.printStackTrace();\n} finally {\n if (scanner != null) {\n scanner.close();\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"// try-with-resources\ntry (Scanner scanner = new Scanner(new File(\"D:/demo.txt\"))) {\n while (scanner.hasNext()) {\n System.out.println(scanner.nextLine());\n }\n} catch (FileNotFoundException e) {\n e.printStackTrace();\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. 異常處理","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Java 中,異常處理機制分爲 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"聲明異常、拋出異常和捕獲異常","attrs":{}},{"type":"text","text":",根據異常的情況,可以對異常進行不同處理:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9a/9ac4333f64fdd63a8a950de0fbd27169.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.1 聲明異常","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於知道如何進行處理的異常,一般要進行捕獲,但此時不知道如何將處理的異常繼續傳遞下去,可以通過在方法簽名中使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"throws","attrs":{}}],"attrs":{}},{"type":"text","text":" 來聲明可能拋出的異常,有如下兩點需要注意:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"非受檢異常(Error、RuntimeException 及其子類)","attrs":{}},{"type":"text","text":" 不能使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"throws","attrs":{}}],"attrs":{}},{"type":"text","text":" 關鍵字來聲明要拋出的異常;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"一個方法出現編譯時異常,就需要 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"try...catch/throws","attrs":{}}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 進行處理,否則會導致編譯失敗","attrs":{}},{"type":"text","text":" ;","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.2 拋出異常","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦覺得某些異常無法處理,但同時又不用我們進行處理,那我們就可以將其拋出。一般是使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"throw","attrs":{}}],"attrs":{}},{"type":"text","text":" 在方法內部拋出一個 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Throwable","attrs":{}}],"attrs":{}},{"type":"text","text":" 類型的異常。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.3 捕獲異常","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"程序在運行前一般不會報錯,但是運行後可能出現某些未知錯誤,如果不想直接拋出給上一級處理,那我們就需要通過 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"try...catch...","attrs":{}}],"attrs":{}},{"type":"text","text":" 的形式對異常進行捕獲,然後根據不同的情況來進行相應處理。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3. 異常常見面試題","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.1 Error 和 Exception 的區別?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Exception","attrs":{}}],"attrs":{}},{"type":"text","text":" 類的異常能夠在程序中進行捕獲並處理,遇到該類異常,應該進行處理,從而使程序能夠繼續正常運行;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Error","attrs":{}}],"attrs":{}},{"type":"text","text":" 類的錯誤一般是虛擬機相關錯誤,如系統崩潰、內存不足、堆棧溢出等,編譯器不會檢測這類錯誤。我們也不會對這類錯誤進行捕獲,一旦發生,一般都會導致程序崩潰無法恢復;","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.2 運行時異常和受檢異常的區別?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"運行時異常包括 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RuntimeException","attrs":{}}],"attrs":{}},{"type":"text","text":" 及其子類,表示 JVM 運行期間可能出現的異常,不會被 Java 編譯器檢查。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而受檢異常是除開 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RuntimeException","attrs":{}}],"attrs":{}},{"type":"text","text":" 及其子類之外的其他 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Exception","attrs":{}}],"attrs":{}},{"type":"text","text":",會被 Java 編譯器檢查。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"兩者的 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"區別","attrs":{}},{"type":"text","text":" 在於:是否需要調用者必須處理該異常,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"如果必須處理,則一般使用受檢異常,否則一般選擇非受檢異常(RuntimeException)","attrs":{}},{"type":"text","text":";","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.3 throw 和 throws 的區別?","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"throw:用於在方法內部拋出異常對象","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"throw","attrs":{}}],"attrs":{}},{"type":"text","text":" 用在方法體內,表示拋出異常,由方法體內的語句處理;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"throw","attrs":{}}],"attrs":{}},{"type":"text","text":" 是具體向外拋出異常的動作,所以拋出的是一個異常實例,執行 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"throw","attrs":{}}],"attrs":{}},{"type":"text","text":" 一定是拋出了某種異常;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"throws:用於在方法簽名上聲明該方法所要拋出的異常","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"throws","attrs":{}}],"attrs":{}},{"type":"text","text":" 語句使用在方法聲明後,表示若拋出異常,則由該方法的調用者來進行異常的處理;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"throws","attrs":{}}],"attrs":{}},{"type":"text","text":" 主要是聲明這個方法會拋出某種類型的異常,讓它的使用者要知道需要捕獲的異常的類型;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"throws","attrs":{}}],"attrs":{}},{"type":"text","text":" 表示出現異常的一種可能性,並非一定發生該種異常;","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.4 final、finally、finallize 的區別?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"final","attrs":{}}],"attrs":{}},{"type":"text","text":" 用於修飾類、方法、變量,修飾類時表示類不能被繼承;修飾方法時表示方法不能別重寫,但是能夠被重載;修飾變量時表示該變量是一個常量無法被重寫賦值;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"finally","attrs":{}}],"attrs":{}},{"type":"text","text":" 一般作用於 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"try...catch","attrs":{}}],"attrs":{}},{"type":"text","text":" 代碼塊,處理異常時,通常將必須要執行的代碼放在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"finally","attrs":{}}],"attrs":{}},{"type":"text","text":" 代碼塊中,表示無論是否出現異常,此代碼塊均執行,一般用來存放一些關閉資源的代碼;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"finallize","attrs":{}}],"attrs":{}},{"type":"text","text":" 是一個方法,屬於 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Object","attrs":{}}],"attrs":{}},{"type":"text","text":" 類,Java 允許用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"finallize()","attrs":{}}],"attrs":{}},{"type":"text","text":" 方法在垃圾回收器將對象從內存中清除前做一些必要的清理工作;","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.5 常見的 RuntimeException 異常?","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"ClassCastException","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"IndexOutOfBoundsException","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"NullPointerException","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"ArrayStoreException","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"BufferOverFlowException","attrs":{}}],"attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.6 JVM 如何處理異常?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦某方法發生異常,該方法就會創建一個異常對象,並將其轉交給 JVM,該異常對象一般包含 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"異常名稱、異常描述以及異常發生時應用程序的狀態","attrs":{}},{"type":"text","text":"。這個 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"創建異常對象並轉交給 JVM 的過程叫做拋出異常","attrs":{}},{"type":"text","text":"。可能有一系列的方法調用,最終才能進入拋出異常的方法,這一系列方法調用的有序列表叫做調用棧。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"JVM 沿着調用棧去查找是否有需要處理異常的代碼,一旦發現則調用異常處理代碼。當 JVM 發現可以處理異常的代碼時,會將發生的異常傳遞給它。如果 JVM 未找到能夠處理該異常的代碼塊,就會將其轉交給默認的異常處理器(JVM 的一部分),由異常處理器打印出異常信息並終止應用程序;","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章