class TestExceptions {
public static void main(String[] args) throws Exception {
try {
System.out.println("try");
throw new Exception();
} catch(Exception e) {
System.out.println("catch");
throw new RuntimeException();
} finally {
System.out.println("finally");
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我尝试多次在eclipse中运行代码时的输出.我相信到目前为止,无论何时执行try/catch块的代码的最后一行(可以返回或抛出新的Exception()类型的stmt),最后都会执行块,但这里的输出不同每次?任何人都可以澄清我的假设是对还是错?
try
catch
Exception in thread "main" finally
java.lang.RuntimeException
at TestExceptions.main(TestExceptions.java:9)
Exception in thread "main" try
catch
java.lang.RuntimeException
at TestExceptions.main(TestExceptions.java:9)
finally
Run Code Online (Sandbox Code Playgroud)
这显然是因为eclipse正在打印error stream并且output stream在控制台中没有正确的同步.很多人因此而看到了问题.
在命令提示符下执行程序,每次都会看到正确的输出.
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |