Try…catch…finally与直接throw的区别:try catch是直接处理,处理完成之后程序继续往下执行,throw则是将异常抛给它的上一级处理,程序便不往下执行了。
public class ZeroTest {
public static void main(String[] args) {
try{
int i = 100 / 0;
System.out.print(i);
}catch(Exception e){
System.out.print(1);
throw new RuntimeException();
}finally{
System.out.print(2);
}
System.out.print(3);
}
}
所以此条程序输出12