当前位置: 代码迷 >> Java相关 >> java中错误处理顺序有关问题!不同次执行结果不同
  详细解决方案

java中错误处理顺序有关问题!不同次执行结果不同

热度:324   发布时间:2016-04-22 21:45:07.0
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 顺序不同
  相关解决方案