在异常捕获中
try{
}catch(){
}finally{
语句1
}
语句2老师说finally中的代码,无论异常不异常都会执行。可是语句2也是无论有没有捕获到异常也会顺序执行下来啊,那finally岂不是可以在任何情况下都没必要用?请高人指点~
try{
}catch(){
}finally{
语句1
}
语句2老师说finally中的代码,无论异常不异常都会执行。可是语句2也是无论有没有捕获到异常也会顺序执行下来啊,那finally岂不是可以在任何情况下都没必要用?请高人指点~
比如
try {
} catch (**Exception ex) {} finally {
return;
}这时try ...catch块之后的代码肯定不会执行的吧。呵呵。
就是执行了finally子句以后,程序要回到抛出异常的地方,而不是执行finally后面的语句.
}catch(){
}finally{
语句1
}
语句2
语句2在TRY里面存在异常时候是不会运行的
你可以试下在TRY里面 throw new IllegalAccessException("自编例外");
语句1无论是否存在异常都会执行
try{
System.out.println("before throw exception");
throw new IllegalAccessException("new exception");
System.out.println("Behind throw exception");
}
catch(){
System.out.println("e.printStackTrace()");
}
finally{
System.out.println("run finally");}
System.out.println("out of try");
晕了 catch里面应该这样写 LZ您试下就知道了 然后在把throw new IllegalAccessException("new exception");去掉在运行下就明白 TRY是怎么用的了
System.exit(0);
}
catch {}
finally{}
此时finally语句好像也不执行
System.out.println("test1");
return;
// Integer.parseInt("a");
// System.out.println("test2");
}catch(Exception e){
System.out.println("test3");
}finally{
System.out.println("test4");
}
System.out.println("test5");
test1
test4