先來一塊兒看看下面的代碼:java
package com.huangzx.Exception;
/** * @author huangzx * @date 2018/11/27 */
public class ExceptionTypeTest {
public class ExceptionTypeTest {
public int doSomething() throws ArithmeticException {
return 5 / 0;
}
public static void main(String[] args) {
ExceptionTypeTest ett = new ExceptionTypeTest();
ett.doSomething();
}
}
複製代碼
問:上面的代碼能編譯經過嗎?程序員
答:能夠。ArithmeticException是RuntimeException異常,能夠不進行捕獲或拋出。 若是改成IOException,以下:測試
package com.huangzx.Exception;
import java.io.*;
/** * @author huangzx * @date 2018/11/27 */
public class ExceptionTypeTest {
public void doSomething() throws IOException {
InputStream is = new FileInputStream(new File("/Users/huangzx/test.txt"));
is.read();
}
public static void main(String[] args) {
ExceptionTypeTest ett = new ExceptionTypeTest();
ett.doSomething();
}
}
複製代碼
問:它還能編譯經過嗎?優化
答:不能。IOException是直接繼承自Exception的異常,它必須獲得處理,要麼捕獲要麼拋出。this
上面兩個例子中,ArithmeticException與IOException都是來自Exception體系,爲何一個不須要處理,一個須要處理呢?這就涉及到兩個概念:受檢異常和非受檢異常。 先來複習一下Java異常體系:spa
二者的表明人物出場(掌聲有請~~):code
既然二者的概念清晰了,處理方式也隨之昭然若揭。cdn
針對受檢異常:blog
針對非受檢異常:繼承
以上是打基礎,下面玩點高級的。
當須要一些跟特定業務相關的異常信息類時,咱們能夠在Java自身定義的異常以外,編寫繼承自Exception的受檢異常類,也能夠編寫繼承自RuntimeException或其子類的非受檢異常類。
通常狀況下,異常類提供了默認構造器和一個帶有String類型參數的構造器。咱們的自定義異常,只須要實現這兩個構造器就足夠用了。由於最有價值的是咱們定義的異常類型(即異常的名字),當異常發生時,咱們只要看到了這個名字就知道發生了什麼。因此除非有一些特殊操做,不然自定義異常只需簡單實現構造器便可。
示例:
一、自定義業務異常類,繼承自RuntimeException。
package com.huangzx.Exception;
/** * @author huangzx * @date 2018/11/27 */
public class BusinessException extends RuntimeException {
private String code; // 異常返回碼
private String msg; // 異常信息
public BusinessException() {
super();
}
public BusinessException(String message) {
super(message);
this.code = code;
}
public BusinessException(String code, String msg) {
super();
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
複製代碼
二、定義方法,聲明throws這個自定義異常。要使用它,必須通知調用代碼的類,作好「抱接」這個異常的心理準備。代碼邏輯中異常發生的點,須要throw拋出。
public void logicCode() throws BusinessException {
throw new BusinessException("-1000", "業務出錯");
}
複製代碼
三、測試自定義異常main方法
public static void main(String[] args) {
ExceptionTest et = new ExceptionTest();
try {
et.logicCode();
} catch (BusinessException e) {
e.printStackTrace();
System.out.println("code=" + e.getCode() + "msg=" + e.getMsg());
}
}
複製代碼
基本上,就差很少了。另外還有一個須要注意的點,就是:千萬不要在finally再拋出一個很是規異常,由於它一定會執行,致使你try{}監測的代碼塊捕獲的異常得不處處理,會起到混淆視聽的效果,讓你排查問題時摸不着頭腦,請切記。示例以下:
package com.huangzx.Exception;
/** * @author huangzx * @date 2018/11/27 */
public class ExceptionLoseTest {
public static int throwException() throws Exception {
try {
throw new Exception();
} catch (Exception e) {
throw e;
} finally {
throw new NullPointerException();
}
}
public static void main(String[] args) {
try {
throwException();
} catch (Exception e) {
e.printStackTrace();
}
}
}
複製代碼
執行結果以下:
java.lang.NullPointerException
at com.huangzx.Exception.ExceptionLoseTest.throwException(ExceptionLoseTest.java:14)
at com.huangzx.Exception.ExceptionLoseTest.main(ExceptionLoseTest.java:20)
複製代碼
好了,下課。。。(老師~再見~~)