【发布时间】:2013-07-24 04:21:01
【问题描述】:
当我运行下面的代码时,会抛出 OutOfMemoryError,但没有被捕获,尽管有一些子句应该捕获它:
public class SomeClass {
public static void main(String[] args) {
try {
//Allocate a huge amount of memory here
} catch (OutOfMemoryError oome) {
System.out.println("OutOfMemoryError=<" + oome + ">");
} catch (Error err) {
System.out.println("Error=<" + err + ">");
} catch (Throwable t) {
System.out.println("Throwable=<" + t + ">");
} finally {
System.out.println("'Finally' clause triggered");
}
}
}
输出如下:
'Finally' clause triggered
Exception in thread "main"
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"
异常未被捕获的事实对我来说毫无意义。 OutOfMemoryError documentation 确认异常应该被 Throwable、Error 或 OutOfMemoryError 捕获。 (请注意,“java.lang.”说明符不会影响行为。)
我在 StackOverflow 上看到的所有其他问题都是“为什么我的 OutOfMemoryError 异常没有被捕获?”是由只捕获 Exception 对象而不是 Error 或 Throwable 的人发布的。
知道发生了什么吗?
【问题讨论】:
-
它看起来也让我吃惊。可能是其他原因,例如OOME gettign 被扔到您期望的其他地方,因此它不会被抓住?检查堆栈跟踪可能会有所帮助。
-
有帮助:stackoverflow.com/questions/5813614/…
Throwable错误很难恢复。 -
测试 Zim-Zam O'Pootertoot 的想法并删除“+ err”并像 finally 一样执行一个字符串,看看你得到了什么结果。
-
好建议,@user1132959!当我不尝试写出异常时,我看到 OutOfMemoryError 子句捕获了它。
标签: java out-of-memory