【发布时间】:2013-02-06 08:19:12
【问题描述】:
给定一段java代码:
class SampleExpcetion {
public static void main(String args[]){
try {
int a[]= new int[15];
a[5]= 30/0;
}
catch(Exception e) {System.out.println("task completed");}
catch(ArithmeticException e) {System.out.println("task1 completed");}
catch(ArrayIndexOutOfBoundsException e) { System.out.println("task2 completed");}
System.out.println("Rest of the code......");
}
}
为什么这段代码会给出编译时错误?
【问题讨论】:
-
哪个错误?我怀疑编译器检测到除以零。
-
你为什么要这样做? a[5]= 30/0 ?
-
合并以下答案的快速说明,当捕获异常时,请务必从最具体的(
ArrayIndexBoundsException等)转移到最通用的(Exception)。以另一种方式进行(就像您正在做的那样)将导致第一个捕获捕获所有内容。
标签: java exception compile-time