[英]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.