# 狂神說Java學習筆記 : 異常 *** > **即使再小的帆也能遠航** >**本篇學習筆記參考自[狂神說](https://www.bilibili.com/video/BV12J41137hu/)** >###### tags: `狂神說Java` `Java基礎` *** # Error 和 Exception - Error 通常是災難性的致命錯誤,是程序無法控制和處理的,當出現這些異常時,Java虛擬機(JVM)一般會選擇終止線程 - Exception通常情況下是可以被程序處理的,並且在程序中應該盡可能地去處理這些異常。  --- # 捕獲和拋出異常 - 異常處理五大關鍵字 1. **try** - 監控區域 2. **catch** - catch(想要捕獲的異常類型) - 捕獲異常 3. **finally** - 處理善後工作 - Optional, 可以不用 finally,但常用於關閉IO、資源 4. **throw** - 主動地拋出異常,一般在方法中使用 5. **throws** - 在方法中,無法處理異常,在方法上拋出異常 - 假設要捕獲多個異常,需要**從小到大**捕獲 - throw/throws 一般用於方法中,拋出異常後會強行結束當前方法(break/return),並繼續執行主程式,會搭配try/catch對應解決異常 ```java= public class Test { public static void main(String[] args) { int a = 1; int b = 0; try{ // 監控區域 System.out.println(a/b); } catch(ArithmeticException e){ // catch(想要捕獲的異常類型) 捕獲異常 System.out.println("程序出現異常,變量b不能為0"); } finally{ // 處理善後工作 System.out.println("finally"); } // 可以不用finally,假設IO、資源關閉 System.out.println("============="); try{ System.out.println(a/b); } catch(Error e){ System.out.println("Error"); } catch(Exception e){ System.out.println("Exception"); } catch(Throwable t){ System.out.println("Throwable"); } finally{ System.out.println("finally"); } System.out.println("============="); try{ new Test().a(); } catch(Throwable e){ System.out.println("程序出現異常"); } finally{ System.out.println("finally"); } /* 程序出現異常,變量b不能為0 finally ============= Exception finally ============= 程序出現異常 finally */ } public void a(){ b(); }; public void b(){ a(); }; } ``` ```java= package com.exception; public class Test2 { public static void main(String[] args) { try { new Test2().test(1, 0); } catch (Exception e) { e.printStackTrace(); // 打印錯誤的棧信息 } finally{ } } // 假設在方法中,無法處理異常,在方法上拋出異常 public void test(int a, int b) throws ArithmeticException{ if (b==0){ // throw throws throw new ArithmeticException(); // 主動地拋出異常,一般在方法中使用 } } } ``` --- # 自定義異常及經驗小結 - extends Exception ---> 自定義異常類 ```java= package com.exception.demo02; // extends Exception ---> 自定義異常類 public class MyException extends Exception { // 傳遞數字>10,拋出異常 private int detail; public MyException(int a) { this.detail = a; } // toString: 異常的打印信息 @Override public String toString() { return "MyException [detail=" + detail + "]"; } } ``` ```java= // 測試 package com.exception.demo02; public class Test { public static void main(String[] args) { try { test(11); } catch (MyException e) { // 增加一些處理異常的代碼... System.out.println("My Exception =>"+e); // 這邊 +e,調用了MyException的ToString // 傳遞的參數為:11 // My Exception =>MyException [detail=11] } } // 可能存在異常的方法 static void test(int a) throws MyException{ System.out.println("傳遞的參數為:" + a); if (a>10){ throw new MyException(a); } System.out.println("OK"); } } ``` ## 經驗總結 - 處理運行異常時,採用邏輯去合理歸避,同時輔助 try-catch 處理 - 使用多重catch時,可以加一個 **catch(Exception)** 來處理可能會被遺漏的異常 - 對於不確定的代碼,也可以加上 try-catch,處理潛在異常 - 盡量去處理異常,切忌只是簡單地調用printStackTrace()去打印輸出 - 具體如何處理異常,要根據不同的業務需求和異常類型去決定 - 盡量添加 finally 語句塊去釋放占用的資源
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up