关于finally块抛出和重新抛出异常时程序流程的问题
Question about program flow when throwing and rethrowing exception with finally block
public class UsingExceptions {
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 throwExceptioin ");
throw e;
}
finally{
System.err.println("Finally executed in method throwException");
}
}
public static void doesNotThrowException(){
try{
System.out.println("Method doesNotThrowException");
}
catch(Exception e){
System.err.println("Exception handled in method doesNotThrowException");
}
finally{
System.out.println("finally executed in doesNotThrowException");
}
System.out.println("end of method doesNotThrowException");
}
}
我的IDE正在输出:
- 方法抛出异常
- 在方法 throwExceptioin 中处理的异常
- 方法不抛出异常
- 最后在throwException方法中执行
- main 中处理的异常
- 终于在doesNotThrowException中执行
- 方法结束 doesNotThrowException
但我期待:
- 方法抛出异常
- 在方法 throwException 中处理的异常
- 最后在throwException中执行
- main 中处理的异常
- 方法不抛出异常
- 最后在doesNotThrowException中执行
- 方法结束 doesNotThrowException
我的问题在哪里?
代码有多个输出。
用eclipse编译:
Exception Handled in Method throwExceptioin
Method throwException
Finally executed in method throwException
Exception handled in main
Method doesNotThrowException
finally executed in doesNotThrowException
end of method doesNotThrowException
通过 Windows 使用标准 javac 编译器编译 cmd.exe:
Method throwException
Exception Handled in Method throwExceptioin
Finally executed in method throwException
Exception handled in main
Method doesNotThrowException
finally executed in doesNotThrowException
end of method doesNotThrowException
使用 IntelliJ 编译 IDEA:
Method throwException
Method doesNotThrowException
finally executed in doesNotThrowException
end of method doesNotThrowException
Exception Handled in Method throwExceptioin
Finally executed in method throwException
Exception handled in main
作为用户 Sorin statet。 IDEs 以不同方式处理打印到控制台。 IntelliJ IDEA 首先输出所有的 System.out.println()
然后是所有的 System.err.println()
。标准 java 编译器以预期的顺序对它们进行线程处理,并以某种奇怪的顺序对它们进行处理。
如果您想要在每个 IDE 中使用您期望的代码,您将不得不使用 System.out.println()
或 System.err.println()
。你也可以看看 this answer
public class UsingExceptions {
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 throwExceptioin ");
throw e;
}
finally{
System.err.println("Finally executed in method throwException");
}
}
public static void doesNotThrowException(){
try{
System.out.println("Method doesNotThrowException");
}
catch(Exception e){
System.err.println("Exception handled in method doesNotThrowException");
}
finally{
System.out.println("finally executed in doesNotThrowException");
}
System.out.println("end of method doesNotThrowException");
}
}
我的IDE正在输出:
- 方法抛出异常
- 在方法 throwExceptioin 中处理的异常
- 方法不抛出异常
- 最后在throwException方法中执行
- main 中处理的异常
- 终于在doesNotThrowException中执行
- 方法结束 doesNotThrowException
但我期待:
- 方法抛出异常
- 在方法 throwException 中处理的异常
- 最后在throwException中执行
- main 中处理的异常
- 方法不抛出异常
- 最后在doesNotThrowException中执行
- 方法结束 doesNotThrowException
我的问题在哪里?
代码有多个输出。
用eclipse编译:
Exception Handled in Method throwExceptioin
Method throwException
Finally executed in method throwException
Exception handled in main
Method doesNotThrowException
finally executed in doesNotThrowException
end of method doesNotThrowException
通过 Windows 使用标准 javac 编译器编译 cmd.exe:
Method throwException
Exception Handled in Method throwExceptioin
Finally executed in method throwException
Exception handled in main
Method doesNotThrowException
finally executed in doesNotThrowException
end of method doesNotThrowException
使用 IntelliJ 编译 IDEA:
Method throwException
Method doesNotThrowException
finally executed in doesNotThrowException
end of method doesNotThrowException
Exception Handled in Method throwExceptioin
Finally executed in method throwException
Exception handled in main
作为用户 Sorin statet。 IDEs 以不同方式处理打印到控制台。 IntelliJ IDEA 首先输出所有的 System.out.println()
然后是所有的 System.err.println()
。标准 java 编译器以预期的顺序对它们进行线程处理,并以某种奇怪的顺序对它们进行处理。
如果您想要在每个 IDE 中使用您期望的代码,您将不得不使用 System.out.println()
或 System.err.println()
。你也可以看看 this answer