java自定义异常(runtimeException)

版权声明:本文为博主搜索整合文章。 https://blog.csdn.net/weixin_37794901/article/details/80213404

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


猜你喜欢

转载自blog.csdn.net/weixin_37794901/article/details/80213404
今日推荐