try{
//可能出现异常的代码
}catch(Exception e//匹配异常){//可以有多个catch,类似于switch,匹配的异常不能是由大到小,不然后面的异常无法匹配
//出现异常之后采取的行动
}final{//非必需
//无论是否出现异常都要采取的行动
}
异常分为:
快捷操作:选中要抛出异常的语句,按ctrl+alt+t
public class MyException extends Exception{
int detail;
public MyException(int a){
this.detail = a;
}
@Override
public String toString(){
return "MyException{" + detail + ‘}‘;
}
}
================================================
public class Test {
static void test(int a)throws MyException{
if(a>10){
throw new MyException(a);
}
}
public static void main(String[] args) {
int a = 11;
try {
new Test().test(a);
System.out.println("传递的参数是:" + a);
} catch (MyException e) {
System.out.println("MyException==>" + e);
}
}
}
原文:https://www.cnblogs.com/St0n3/p/14471531.html