天天看點

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 處理;

繼續閱讀