在下面的代码片段中,该printStackTrace()方法在catch block.中调用.运行程序后,您可以看到有时printStackTrace()连续几次运行而不是按printStackTrace()- > catch block- > 的顺序运行finally block.
如果您更改static boolean b为false然后按System.out.print(e)顺序执行.
那么为什么printStackTrace()表现方式不同呢?(带线程的东西??)
public class PrintStackTrace {
static boolean b = true;
public static void main(String[] args){
for(int i = 0; i < 100; i++){
try{
throw new Exception("[" + i + "]");
}
catch(Exception e){
if(b){
e.printStackTrace();
}
else{
System.out.print(e);
}
System.out.print(" Catch: " + i);
}
finally{
System.out.print(" Finally: " …Run Code Online (Sandbox Code Playgroud)