为什么没有捕获“java.lang.OutOfMemoryError: Java heap space”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1888602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:23:41  来源:igfitidea点击:

Why is "java.lang.OutOfMemoryError: Java heap space" not caught?

javatry-catchout-of-memory

提问by Chris Persichetti

I have a thread in a Java web application that causes a java.lang.OutOfMemoryError: Java heap spaceexception, but the try/catch block does not catch the error.

我在 Java Web 应用程序中有一个线程会导致java.lang.OutOfMemoryError: Java heap spaceexception,但 try/catch 块没有捕获错误。

Sample code:

示例代码:

private void doSomeWork()
{
     try
     {
         processData();  //Causes OutOfMemoryError
         System.out.println("This line does not execute");
     }
     catch (Exception e)
     {
         System.out.println("Exception.  This line does not execute.");
         //Log error
     }
     finally
     {
         System.out.println("finally.  This line does execute");
         System.out.println("Thread name: " + Thread.currentThread().getName());

     }
}

Output:

输出:

finally.  This line does execute 
Thread name: _Worker-8
Exception in thread "_Worker-8" java.lang.OutOfMemoryError: Java heap space
...

Background:

背景:

I recently took over this Java project and I'm trying to get up to speed with Java and this project. I'm a C# developer, so I'm not yet familiar with this project or Java. I know I can fix the error using the -Xmx setting, but I'm interested in catching this error so I can log it. The error is not showing up in any of the logs files, and the output is being shown in the console in debug mode in Eclipse.

我最近接手了这个 Java 项目,我正在努力跟上 Java 和这个项目的步伐。我是一名 C# 开发人员,所以我还不熟悉这个项目或 Java。我知道我可以使用 -Xmx 设置修复错误,但我有兴趣捕获此错误以便我可以记录它。该错误未显示在任何日志文件中,并且输出以 Eclipse 的调试模式显示在控制台中。

回答by Kaleb Brasee

Because OutOfMemoryErroris an Error, not an Exception. Since OutOfMemoryErrorisn't a subclass of Exception, the catch (Exception e)doesn't apply.

因为OutOfMemoryErrorError,不是Exception。由于OutOfMemoryError不是 的子类Exception,因此catch (Exception e)不适用。

OutOfMemoryErrordoes extend Throwable, however, so you should be able to catch it. Here's a SO discussionon when (if ever) you should catch Errors. Generally, since you can't do anything about it, the recommendation is to not bother catching Errors in production code. But given a special case where you're trying to debug what's going on, it might be helpful.

OutOfMemoryErrorThrowable但是,确实扩展了,所以你应该能够抓住它。这是关于何时(如果有的话)您应该捕获错误的SO 讨论。通常,由于您对此无能为力,因此建议不要打扰在生产代码中捕获错误。但是考虑到您尝试调试正在发生的事情的特殊情况,它可能会有所帮助。

回答by Massimo Fazzolari

java.lang.OutOfMemoryError doesn't extends java.lang.Exception so it's not an Exception. OutOfMemoryError extends java.lang.Error. If you want to catch Error try this:

java.lang.OutOfMemoryError 不扩展 java.lang.Exception 所以它不是一个异常。OutOfMemoryError 扩展了 java.lang.Error。如果你想捕捉错误试试这个:

private void doSomeWork()
{
     try
     {
         processData();  //Causes OutOfMemoryError
         System.out.println("This line does not execute");
     }
     catch (Error e)
     {
         System.out.println("Exception.  This line does not execute.");
         //Log error
     }
     finally
     {
         System.out.println("finally.  This line does execute");
         System.out.println("Thread name: " + Thread.currentThread().getName());

     }
}

Note: Exception and Error extends Throwable so you you can also use Throwable to catch both.

注意:Exception 和 Error 扩展了 Throwable,因此您也可以使用 Throwable 来捕获两者。

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Throwable.html

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Throwable.html

回答by Dennis C

An "~Error" is not an "~Exception".

“~Error”不是“~Exception”。

You have to catch "Error" or "Throwable"

您必须捕获“错误”或“可抛出”

回答by Daniil

OutOfMemoryError extends VirtualMachineError while Exception extends Throwable directly. So it is not being caught as per Java specs. IF you're looking to catch all exceptions, add catch (Throwable e) to the clause and you'll have it.

OutOfMemoryError 扩展了 VirtualMachineError 而 Exception 直接扩展了 Throwable。所以它没有按照 Java 规范被捕获。如果您希望捕获所有异常,请将 catch (Throwable e) 添加到子句中,您就会拥有它。

回答by Javamann

What I will usually do is add an 'UncaughtExceptionHandler' to a Thread so if anything leaks by you at least have a chance to log the issue and maybe do some cleanup.

我通常会做的是向线程添加一个“UncaughtExceptionHandler”,这样如果您有任何泄漏,至少有机会记录问题并进行一些清理。