为什么我们不应该在 Java 中捕获错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15242207/
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
Why should not we catch Errors in Java?
提问by lies
I know, that it harms application performance, but should not we surround places of code, which really risky with e.g.
我知道,它会损害应用程序的性能,但我们不应该包围代码的地方,这真的很危险,例如
try {
// code
} catch (OutOfMemoryError ex) {
// handling code
}
It looks pretty safe.
它看起来很安全。
From docs:
从文档:
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
Error 是 Throwable 的一个子类,它指示合理的应用程序不应尝试捕获的严重问题。
Why not?
为什么不?
回答by Sean Landsman
The reason that you shouldnt generally attempt to handle these errors is because more often than not there wont be anything at all you can do about them.
您通常不应该尝试处理这些错误的原因是因为通常情况下您对它们无能为力。
They will tend to be JVM level errors, not application level ones - OutOfMemory is a good example here. If the JVM has run out of memory what would your program do? And even if you did catch it there is no guarentee that the handling code would complete/proceed in a consistent manner, given the terminal condition thrown
它们往往是 JVM 级别的错误,而不是应用程序级别的错误 - OutOfMemory 就是一个很好的例子。如果 JVM 内存不足,您的程序会做什么?即使你确实抓住了它,也不能保证处理代码会以一致的方式完成/继续,考虑到抛出的终端条件
回答by ggcodes
You can catch anything that is Throwable which means you can catch Error. But an Error represents a serious problem and not advisable to catch.
你可以捕捉到任何可以抛出的东西,这意味着你可以捕捉到错误。但是错误代表一个严重的问题,不建议捕获。
From Java API:
"An Error is a subclass of Throwable that indicates serious problems that a reasonable >application should not try to catch. Most such errors are abnormal conditions. The >ThreadDeath error, though a "normal" condition, is also a subclass of Error because most >applications should not try to catch it."
来自 Java API:
“一个 Error 是 Throwable 的一个子类,它表明一个合理的 > 应用程序不应该试图捕捉的严重问题。大多数这样的错误是异常情况。>ThreadDeath 错误虽然是一个“正常”的情况,但也是 Error 的一个子类,因为大多数>应用程序不应试图捕捉它。”
Errorrepresents some critical problem with your application. For example OutOfMemoryErrorwill be thrown when the Java Virtual Machine
cannot allocatean object because it is out of memory, and no more memory could be made available by the garbage collector. So if by catching OutOfMemoryError
will not solve your actual problems except increasing the memory
and by catching this Error in program may not lead any solution and rather causes error prone application. So, it is not advisable to catch errors.
错误表示您的应用程序存在一些严重问题。例如,当对象因内存不足而无法分配时,将抛出OutOfMemoryError,并且垃圾收集器无法提供更多可用内存。因此,如果通过捕获不会解决您的实际问题,除非通过捕获程序中的此错误可能不会导致任何解决方案,而是会导致容易出错的应用程序。因此,不建议捕获错误。Java Virtual Machine
OutOfMemoryError
increasing the memory
回答by Nicolas Zozol
There is an Error when the JVM is no more working as expected, or is on the verge to. If you catch an error, there is no guarantee that the catch block will run, and even less that it will run till the end.
当 JVM 不再按预期工作或即将工作时,会出现错误。如果捕获错误,则无法保证 catch 块会运行,更不能保证它会运行到最后。
It will also depend on the running computer, the current memory state, so there is no way to test, try and do your best. You will only have an hasardous result.
它还取决于正在运行的计算机,当前的内存状态,因此无法测试,尝试并尽力而为。你只会有一个危险的结果。
You will also downgrade the readability of your code.
您还将降低代码的可读性。
回答by NPKR
The Error
types actually we can't guess this will come perticular block of Code
like below.
Error
实际上我们无法猜测的类型会perticular block of Code
如下所示。
try {
// not guaranty OutOf memeroy Exception will come from this block
} catch (OutOfMemoryError ex) {
// handling code
}
That's why we will not handle.
这就是为什么我们不会处理。
回答by Arsen Alexanyan
What's the meaning to catch OutOfMemoryError and do some work in your catch block if your heap space or perm gen space is full? Actually You cannot guess where can this exception actually happen. There is no logic to catch this exception.
如果您的堆空间或永久空间已满,捕获 OutOfMemoryError 并在您的 catch 块中做一些工作是什么意思?实际上,您无法猜测此异常实际上在哪里发生。没有逻辑可以捕获此异常。