自定义异常
作者:互联网
package com.liu.exception;
public class MyException extends Exception{
//传递数字大于10启用自定义异常
private int detail;
public MyException(int a){
this.detail =a;
}
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}
package com.liu.exception;
public class MyEx {
//可能存在异常的方法
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) {
e.printStackTrace();
} finally {
}
}
}
标签:自定义,int,MyException,detail,System,异常,public 来源: https://www.cnblogs.com/sm12580/p/15313498.html