自定义异常
import java.lang.Throwable;
class Test{
public int divide(int x,int y) throws Exception{
if(y < 0){//自定义异常
throw new DingYiException("除数 is 负数" +y);
}
else
return x/y;
}
}
class DingYiException extends Exception {
public DingYiException(String msg){
super(msg);
}
}
class TestInner1{
public static void main(String[]args) {//throws Exception写在这里就不用try catch进行扑火
try {
new Test().divide(6,-1);
}
catch(ArithmeticException e){
e.printStackTrace();
}
catch(DingYiException e){//ArithmeticException,这里的e可以换成ex
//e = printStackTrace();将出错的信息直接打在屏幕上
System.out.print("定义异常"+e.getMessage());//这里面还是要写内容的,因为出错你根本觉察不到,以防以后出错
}
catch(Exception e){
e.printStackTrace();
}
finally{
System.out.println("PROGRAM IS HERE");
}
}
}