抛异常:
public class ExceptionTest {
public static Integer i = 0;
public static void main(String[] args) throws Exception{
try{
if(i != 0)
throw new Exception("sadlfj");
}catch(NullPointerException e){
System.out.println("Caught NullPointerException111");
}finally{
System.out.println("Got through new");
}
try{
i = 10;
System.out.println(i.toString());
}catch(NullPointerException e){
System.err.println("Caught NullPointerException222");
}finally{
System.out.println("Got through it");
}
}
}
输出结果:
Got through new
10
Got through it
我觉得如果没有在第一个try块中没有抓住异常,就不应在执行第二个try块啊,但是为什么第二个try块也执行了?和finalize有关系么?
------解决方法--------------------------------------------------------
if(i != 0) //现在你设定的是零
throw new Exception("sadlfj"); //所以这条语句不执行