日期:2014-05-20  浏览次数:20658 次

java中异常处理顺序问题!!!不同次执行结果不同
网上看到的一个程序,执行多次结果均不相同。
public class cc{
public static void main(String args[]){
try{
throwException();
}
catch (Exception e){
System.err.println("Exception handled in main");
}
doesNotThrowException();
}
public static void throwException() throws Exception{
try{
System.out.println("Method throwException");
throw new Exception();
}
catch(Exception e){
System.err.println("Exception handled in method throwException");
throw e;
}
finally{
System.err.println("Finally is always executed");
}
}
public static void doesNotThrowException(){
try{
System.out.println("Method doesNotThrowException");
}
catch (Exception e){
System.err.println(e.toString());
}
finally{
System.err.println("Finally is always executed.");
}
}
}
结果一:
Method throwException   
Exception handled in method throwException   
Finally is always executed   
Exception handled in main   
Method doesNotThrowException   
Finally is always executed!!!   

结果二:
Method throwException   
Method doesNotThrowException   
Exception handled in method throwException   
Finally is always executed   
Exception handled in main   
Finally is always executed!!!   

结果三:

Method throwException   
Exception handled in method throwException   
Finally is always executed   
Exception handled in main   
Finally is always executed!!!   
Method doesNotThrowException   
 结果N:
……
多个版本,为什么执行顺序会不同呢,我是在eclipse中编译执行的。
求高手解惑!!!
Java 异常处理 Eclipse finally 顺序不同