1. 程式人生 > >java自定義異常(runtimeException)

java自定義異常(runtimeException)

                    Throwable                      /                \              Error             Exception /                   /               \xxxxxx             xxxxxx          RuntimeException                                                          /                                          \IllegalStateException  ArithmeticException 

demo:

public class MyException extends RuntimeException {


/**

*/
private static final long serialVersionUID = -8716100844379461082L;
private Integer code;//自定義異常碼


public Integer getCode() {
return code;
}


public void setCode(Integer code) {
this.code = code;
}


public MyException(String message, Integer code) {
super(message);// 父類的建構函式;呼叫底層的Throwable的建構函式,將引數message賦值到detailMessage (Throwable的屬性)
this.code = code;//賦值code碼
}

}

使用場景:

    throw new  MyException(code,“message”);
    //有上級函式try catch 處理;