【发布时间】:2013-09-07 15:05:15
【问题描述】:
class chain_exceptions{
public static void main(String args[]){
try
{
f1();
}
catch(IndexOutOfBoundsException e)
{
System.out.println("A");
throw new NullPointerException(); //Line 0
}
catch(NullPointerException e) //Line 1
{
System.out.println("B");
return;
}
catch (Exception e)
{
System.out.println("C");
}
finally
{
System.out.println("D");
}
System.out.println("E");
}
static void f1(){
System.out.println("Start...");
throw new IndexOutOfBoundsException( "parameter" );
}
}
我希望第 1 行捕获从第 0 行抛出的 NullPointerException,但它没有发生。
但是为什么会这样呢?
当定义了另一个catch块时,为什么Line1的NPE处理程序不能捕捉它?
是不是因为“抛出”直接进入了main()方法?
【问题讨论】:
-
你应该阅读一下Java naming conventions。
-
如果您好奇,可以在 Java 规范here 中找到详细信息。
-
异常被嵌套块捕获,而不是同一级别的块。
标签: java