//自定义的异常类
public class MyException extends Exception{
//双击shift:搜索
//传递数字>10
private int detail;
public MyException(int a){
this.detail = a;
}
//toString:异常的打印信息
@Override
public String toString(){
return "MyException{" + "detail=" + detail + '}';
}
}
public class TestMy {标签:自定义,int,MyException,detail,异常,public,oppdemo12 From: https://www.cnblogs.com/123jgh/p/16787324.html
//可能会存在异常的方法
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(5);
} catch (MyException e) {
//增加一些处理异常的代码
System.out.println("MyException-->"+e);;
}
}
}