首页 > 其他分享 >自定义的异常类oppdemo12

自定义的异常类oppdemo12

时间:2022-10-13 10:45:20浏览次数:68  
标签:自定义 int MyException detail 异常 public oppdemo12

//自定义的异常类
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 {

//可能会存在异常的方法
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);;
}
}
}

标签:自定义,int,MyException,detail,异常,public,oppdemo12
From: https://www.cnblogs.com/123jgh/p/16787324.html

相关文章