Java(异常)

Java(异常),第1张

Java(异常) 异常

try catch finally throw throwsctrl+alt+T

public class MyException extends Exception {
    //传递数字>10
    private int detail;

    public MyException(int a) {
//        super(message);
        this.detail = a;
    }
//异常的打印信息
    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}
public class Test {
    //可能存在异常的方法
    static void test(int b) throws MyException {
        System.out.println("传递的参数为:");
        if (b>10){
            throw new MyException(b);
        }
        System.out.println("ok");
    }

    public static void main(String[] args) {
        try {
            test(17);
        } catch (MyException e) {
            System.out.println("MyException=>"+e);
        }finally {
            System.out.println("继续");
        }
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5709531.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存