//在异常处理中finally无论发生异常还是不发生异常都会执行,等于对异常检测与处理异常收尾工作
//throws 如果异常在方法体内不能被处理,则将异常抛出方法体,交给方法调用者处理
//当try体内发生异常时,系统将依次匹配与之相对应的异常处理的catch,
//直到执行到finally如果未找到与之相匹配的catch异常处理则将异常抛出方法体
//方法体默认抛出 throws Exception 所有可能发生未被处理的异常
//throws 可以指定抛出具体的异常类型,由被调用者处理
//throw主动抛出异常对象
//***如果在catch中不想处理此对象 则使用throw将该异常抛给调用者去处理
//重写方法 抛出异常必须保持一致
public class ExceptionMain {
public static void main(String[] args)throws NullPointerException{
int[] a = new int[5];
int[] b = {1,2,3,4};
int[] c = new int[]{4,5,6,7};
try{
// System.out.println(0/0);
// a[5] = a[5];
// throw new NullPointerException();
}catch(ArithmeticException e){
System.out.println("数学异常报出");
e.printStackTrace();
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组越界异常报出");
e.printStackTrace();
}catch(Exception e){
System.out.println("无法处理的异常报出");
e.printStackTrace();
}finally{
System.out.println("异常检测通过");
}
}
}
网友评论