异常

异常的祖祖辈辈
在这里插入图片描述
工作中接触的一般都是Exception下的异常,在常规业务处理中,一般会把异常集中归类,例如一个项目中新建一个自定义异常的鼻祖:BaseException ,并且让这个鼻祖继承RuntimeException,构造器也都写上,例如下方:

package com.wx.Exception.custom;

public class BaseException extends RuntimeException {
    public BaseException() {
    }

    public BaseException(String message) {
        super(message);
    }

    public BaseException(String message, Throwable cause) {
        super(message, cause);
    }

    public BaseException(Throwable cause) {
        super(cause);
    }

    public BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

然后呢,其他的业务异常就统统的称这个BaseException为祖先了。

package com.wx.Exception.custom;

public class NoSeeException extends BaseException {
    NoSeeException(String path, String reason) {
        super(path + ((reason == null)
                ? ""
                : " (" + reason + ")"));
    }
}

最后,体验一把喽。

package com.wx.Exception.custom;

public class Test {

    public static void main(String[] args) {
        b();
        System.out.println("111111111111111");

        a();
    }

    public static  String b(){
        String result = "";

        try {
            a();
            System.out.println(">>>>>>>>>>>>>>>>");
        } catch (Exception e) {
            System.out.println("-------------");
            e.printStackTrace();
        }

        return result;
    }

    public static String a() throws NoSeeException{
    throw new NoSeeException("谁动了我的奶酪","没空");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值