什么时候捕获 java.lang.Error? [英] When to catch java.lang.Error?
问题描述
在什么情况下应该在应用程序上捕获java.lang.Error
?
In what situations should one catch java.lang.Error
on an application?
推荐答案
一般来说,从不.
但是,有时您需要捕获特定错误.
However, sometimes you need to catch specific errors.
如果您正在编写类似框架的代码(加载第 3 方类),捕获 LinkageError
(未找到类定义、不满意的链接、不兼容的类更改)可能是明智之举.
If you're writing framework-ish code (loading 3rd party classes), it might be wise to catch LinkageError
(no class def found, unsatisfied link, incompatible class change).
我还看到一些愚蠢的 3rd 方代码抛出 Error
的子类,所以你也必须处理这些.
I've also seen some stupid 3rd-party code throwing subclasses of Error
, so you'll have to handle those as well.
顺便说一下,我不确定是否可以从 OutOfMemoryError
中恢复.
By the way, I'm not sure it isn't possible to recover from OutOfMemoryError
.
这篇关于什么时候捕获 java.lang.Error?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!