简体   繁体   English

尝试打印堆栈跟踪时出现StackOverflowError

[英]StackOverflowError when trying to print a stacktrace

I got the following stacktrace when trying to print a stacktrace to the console: 尝试将stacktrace打印到控制台时,我得到了以下stacktrace:

java.lang.StackOverflowError
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.write(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
at sun.nio.cs.StreamEncoder.flushBuffer(Unknown Source)
at java.io.OutputStreamWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at java.lang.Throwable.printStackTrace(Throwable.java:461)
at java.lang.Throwable.printStackTrace(Throwable.java:451)
...

Has anyone seen something like this before? 以前有人见过这样的东西吗? What could have caused this? 可能是什么导致了这个? (Unfortunately I received the stacktrace from a user, so I can't say whether the JVM configuration was changed in any way.) (不幸的是我收到了用户的堆栈跟踪,所以我不能说JVM配置是否以任何方式被更改。)

The method call that caused this was a simple catch-and-print, like this: 导致这种情况的方法调用是一个简单的catch-and-print,如下所示:

try {
  ...
  File canFile = new File(path).getCanonicalFile();
  ...
} catch (IOException e) {
  e.printStackTrace();
}

Although I can't publish any more code, I can guarantee that the IOException was thrown by the getCanonicalFile call, because that's the only call within the try-clause that can throw an IOException. 虽然我不能再发布任何代码,但我可以保证getCanonicalFile调用抛出了IOException,因为这是try-clause中唯一可以抛出IOException的调用。

If you initialise two Throwable s to be mutual causes of one another, you'll get a StackOverflowError if you attempt to print the stacktrace of one of them. 如果将两个Throwable初始化为彼此的相互原因,如果您尝试打印其中一个的StackOverflowError ,则会得到StackOverflowError

The following class demonstrates this behaviour: 以下类演示了此行为:

public class StackTraceStackOverflow {
    public static void main(String[] args) {
        Error e = new Error();
        Error f = new Error(e);
        e.initCause(f);
        e.printStackTrace();
    }
}

We need two Throwable s in this example because it's not possible to set a Throwable to be its own cause. 在这个例子中我们需要两个Throwable ,因为不可能将Throwable设置为它自己的原因。 If you try to do this, you'll get an IllegalArgumentException . 如果您尝试这样做,您将收到IllegalArgumentException

Alternatively, you can also get a stack-overflow error if you have a ridiculously long chain of cause exceptions. 或者,如果您有一个可笑的长链原因异常,您也可能会收到堆栈溢出错误。 On my machine at least (Kubuntu Natty, x64, OpenJDK 1.6), I found that a chain of 8000 cause exceptions is enough to generate a StackOverflowError , as the following class demonstrates: 至少在我的机器上(Kubuntu Natty,x64,OpenJDK 1.6),我发现8000个引发异常的链足以生成StackOverflowError ,如下面的类所示:

public class StackTraceStackOverflow2 {
    public static void main(String[] args) {
        Error e = null;
        for (int i = 0; i < 8000; ++i) {
            e = new Error(null, e);
        }

        e.printStackTrace(System.out);
    }
}

You may have to adjust the number 8000 on other systems. 您可能需要在其他系统上调整数字8000。

Note that I used the two-argument Error constructor in this second example. 请注意,我在第二个示例中使用了两参数Error构造函数。 If I used the constructor that takes a single Throwable parameter, the exception message is populated using the cause exception. 如果我使用了带有单个Throwable参数的Throwable函数,则使用cause异常填充异常消息。 This message gets longer and longer as the chain of exceptions grows, and as a result you are more likely to end up with an OutOfMemoryError than a StackOverflowError . 随着异常链的增长,此消息变得越来越长,因此您更有可能以StackOverflowError结束OutOfMemoryError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 6-尝试复制和粘贴文件时出现StackOverflowError - Java 6 - StackOverflowError when trying to copy and paste a file 尝试建立与 MySQL 数据库的连接时出现 StackOverflowError - StackOverflowError when trying to establish connection to MySQL database 尝试实现 JWT 时出现 Hibernate 的 StackOverflowError - StackOverflowError with Hibernate when trying to implement JWT Security 尝试将应用程序部署到Weblogic时出现StackOverflowError吗? - StackOverflowError when trying to deploy an application to Weblogic? 尝试使用Neuroph框架训练神经网络时出现StackOverFlowError - StackOverFlowError when trying to train neural network with Neuroph framework Stacktrace打印抑制 - Stacktrace print suppress 如何不在浏览器上打印stacktrace - How not to print stacktrace on the browser 尝试为 DialogFragment 内的 AlertDialog 增加自定义布局时出现 StackOverflowError - StackOverflowError when trying to inflate a custom layout for an AlertDialog inside a DialogFragment 尝试DFS此图时为什么会出现StackOverFlowError? - Why do I get StackOverFlowError when trying to DFS this graph? 尝试使用Jackson从Idml序列化到Json时出现StackOverflowError - StackOverflowError when trying to serialize from Idml to Json with Jackson
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM