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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章