Day16自定义异常(Day16 custom exception)

package com.exception.demo02;//自定义的异常类public class MyException extends Exception{    //传递数字>10抛出异常    private  int data1;    public MyException(int a) {        this.data1=a;    }    //toString:异常的打印信息    @Override    public String toString() {        return "MyException{" + "data1=" + data1 + '}';    }}
package com.exception.demo02;public class Test {    //可能会存在异常的方法    static void test(int a) throws MyException{        System.out.println("传递的参数为:"+a);        if(a>10){            throw new MyException(a);//抛出        }        System.out.println("ok");    }    public static void main(String[] args) {        try {            test(11);        } catch (MyException e) {            //增加一些处理异常的代码            System.out.println("MyException=>"+e);        }    }}
————————
package com.exception.demo02;//自定义的异常类public class MyException extends Exception{    //传递数字>10抛出异常    private  int data1;    public MyException(int a) {        this.data1=a;    }    //toString:异常的打印信息    @Override    public String toString() {        return "MyException{" + "data1=" + data1 + '}';    }}
package com.exception.demo02;public class Test {    //可能会存在异常的方法    static void test(int a) throws MyException{        System.out.println("传递的参数为:"+a);        if(a>10){            throw new MyException(a);//抛出        }        System.out.println("ok");    }    public static void main(String[] args) {        try {            test(11);        } catch (MyException e) {            //增加一些处理异常的代码            System.out.println("MyException=>"+e);        }    }}