java - 是Throwable类的printStackTrace(),异步

标签 java exception asynchronous 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);
    }

}

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

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

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

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

在上面的输出中,堆栈跟踪是在调用它的 catch block 执行之后以及 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);
        }
    }

我试图用谷歌搜索它,但没有找到任何解释。

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

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

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

最佳答案

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

关于java - 是Throwable类的printStackTrace(),异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23738725/

相关文章:

java - JUnit:测试异常不起作用(AssertionError:即使抛出异常,也是预期的异常)

Python 异步 : Running subprocess_exec on a worker thread

Angular 2 ngIf 可观察?

java - Java二叉树节点删除问题

java - Maven 原型(prototype) - 由于冒号导致的速度错误

关于字符串操作的 Java 问题 - 反转文本将 "null"添加到末尾

iOS - 在 main() 中捕获异常

java - 如何获取异常源对象

Swift - 异步/等待不停止让任务完成

Java - 类存在两次(在类路径和应用程序 jar 上)。链接错误: ClassCastException