自定义异常练习

//自定义异常要继承一个异常接口
class MyException extends Exception {
    public MyException() {}
    public MyException(String msg) {
        super(msg);
    }
}
public class ExceptionDemo3 {
    public static void main(String[] args) throws MyException {
        try {
            //test();
            int i = 10/0;
            System.out.println("有没有执行 i="+i);
        } catch (ArithmeticException e) {
            System.out.println("Catch My Exception");
            throw new MyException("我自己定义的错误");
        }
    }
}

在这里插入图片描述

  1. 在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/duanbaoke/article/details/89221399