当我执行此代码时,我得到caughtjava.lang.CloneNotSupportedException
作为输出!为什么NullPointerException
没有被抓住?
package arrays;
public class NestedTry {
public static void main(String s[])
{
try{
try{
throw new NullPointerException();
}
finally{
throw new CloneNotSupportedException();
}
}
catch(Exception e)
{
System.out.println("caught"+e.toString());
//which excpetion will be printed here?? :P
}
}
}
答案 0 :(得分:1)
肯定会执行finally
块。
try{
throw new NullPointerException();
}
finally{
throw new CloneNotSupportedException();
}
在上面的代码中,第一个NullPointerException()
被抛出,但对于try
,finally
块再次抛出CloneNotSupportedException();
所以最终
catch(Exception e)
{
System.out.println("caught"+e.toString());
//which excpetion will be printed here?? :P
}
上述catch
代替NullPointerException
抓取finally
阻止CloneNotSupportedException
并打印caughtjava.lang.CloneNotSupportedException
。