自定义异常

package com.lsx.base;

public class MyException extends Exception {
private int detail;

public MyException(int detail) {
this.detail = detail;
}

@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}

class TestDemo {
public 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(12);
} catch (MyException e) {
System.out.println("MyException==>" + e);
}
}
}
posted @ 2020-07-21 17:49  吾爱开发~  阅读(167)  评论(0编辑  收藏  举报