是Throwable类的printStackTrace()和异步

Ani*_*dia 2 java asynchronous exception stack-trace

我正在测试一些代码来读取类路径中可用的文件,但最后我得到了一些与Throwable类的printStackTrace()方法相关的有趣的东西.

这是代码:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class Main {

    public static void main(String[] args) throws IOException, URISyntaxException {
        try {
            System.out.println("before-test1");
            test1(); // will throw exception
            System.out.println("after-test1");
        } catch (Exception e) {
            System.out.println("entering catch-1");
            e.printStackTrace();
            System.out.println("exiting catch-1");
        }

        try {
            System.out.println("before-test2");
            test2();
            System.out.println("after-test2");
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            System.out.println("before-test3");
            test3();
            System.out.println("after-test3");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void test1() throws IOException {
        String path = "classpath:/test.txt";

        File file = new File(path);

        String content = FileUtils.readFileToString(file, "UTF-8");

        System.out.println("content = " + content);
    }

    private static void test2() throws IOException {
        String path = "/test.txt";
        InputStream in = Main.class.getResourceAsStream(path);

        String content = IOUtils.toString(in);
        System.out.println("content = " + content);
    }

    private static void test3() throws IOException, URISyntaxException {
        String path = "/test.txt";

        URL url = Main.class.getResource(path);

        File file = new File(url.toURI());

        String content = FileUtils.readFileToString(file, "UTF-8");

        System.out.println("content = " + content);
    }

}
Run Code Online (Sandbox Code Playgroud)

test.txt的内容只有一行,如下所示:

anil bharadia

现在这个程序的预期输出如下:

before-test1
entering catch-1
java.io.FileNotFoundException: classpath:\test.txt (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:874)
    at Main.test1(Main.java:50)
    at Main.main(Main.java:16)
exiting catch-1
before-test2
content = anil bharadia
after-test2
before-test3
content = anil bharadia
after-test3
Run Code Online (Sandbox Code Playgroud)

当我第一次运行这个课时,我得到的输出与上面相同.

然后我再次运行同一个类,我得到以下输出:

before-test1
entering catch-1
exiting catch-1
before-test2
content = anil bharadia
after-test2
before-test3
java.io.FileNotFoundException: classpath:\test.txt (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:874)
    at Main.test1(Main.java:50)
    at Main.main(Main.java:16)
content = anil bharadia
after-test3
Run Code Online (Sandbox Code Playgroud)

在上面的输出中,在执行catch块之后打印堆栈跟踪,并且在执行test2()执行结束之后也是如此.

所以这让我认为printStackTrace()方法在某种程度上是异步的.

我试图查看printStackTrace()的源代码,发现以下代码对我没有帮助:

public void printStackTrace(PrintStream s) {
        synchronized (s) {
            s.println(this);
            StackTraceElement[] trace = getOurStackTrace();
            for (int i=0; i < trace.length; i++)
                s.println("\tat " + trace[i]);

            Throwable ourCause = getCause();
            if (ourCause != null)
                ourCause.printStackTraceAsCause(s, trace);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我试图谷歌但没有找到任何解释.

当我试图一次又一次地运行同一个类时,在很多情况下我也发现了以下输出:

java.io.FileNotFoundException: classpath:\test.txt (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:874)
    at Main.test1(Main.java:50)
    at Main.main(Main.java:16)
before-test1
entering catch-1
exiting catch-1
before-test2
content = anil bharadia
after-test2
before-test3
content = anil bharadia
after-test3
Run Code Online (Sandbox Code Playgroud)

这让我想得更多,比如在调用test1()方法之前如何打印堆栈跟踪.这是不可能的.

Eva*_*les 5

堆栈跟踪打印将转到另一个流 - 它们将打印到错误流,而不是标准输出流.这就是为什么有时它们会以不同的顺序显示.