在 try catch 中使用 Throwable 和 Exception 之间的区别
有时,我看到
try {
} catch(Throwable e) {
}
和有时
try {
} catch(Exception e) {
}
有什么区别?
Sometimes, I see
try {
} catch(Throwable e) {
}
And sometimes
try {
} catch(Exception e) {
}
What is the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通过捕获
Throwable
,它包含了Error
子类的内容。通常,您不应该这样做,除非在线程的最高“捕获所有”级别,您想要记录或以其他方式处理绝对可能出错的所有内容。它在框架类型应用程序(例如应用程序服务器或测试框架)中更为典型,其中可以运行未知代码,并且不应受到该代码出现问题的任何影响,如下所示尽可能多。By catching
Throwable
it includes things that subclassError
. You should generally not do that, except perhaps at the very highest "catch all" level of a thread where you want to log or otherwise handle absolutely everything that can go wrong. It would be more typical in a framework type application (for example an application server or a testing framework) where it can be running unknown code and should not be affected by anything that goes wrong with that code, as much as possible.第一个捕获
Throwable
(这包括异常
和Error
),第二个捕获异常
。Error
在编程上是无法以任何方式恢复的,并且通常不会被捕获,除非用于日志记录目的(再次传递它)。Exception
可以通过编程方式恢复。它的子类RuntimeException
表示编程错误,通常也不会被捕获。The first one catches all subclasses of
Throwable
(this includesException
andError
), the second one catches all subclasses ofException
.Error
is programmatically unrecoverable in any way and is usually not to be caught, except for logging purposes (which passes it through again).Exception
is programmatically recoverable. Its subclassRuntimeException
indicates a programming error and is usually not to be caught as well.我觉得这应该在这里:
( img 的 Alt 链接 )
来源:
I feel like this should be here:
( Alt link for the img )
Source: https://www.tutorialspoint.com/java/java_exceptions.htm
Throwable
是Exception
以及Error
的超类。在正常情况下,我们应该始终捕获Exception
的子类,这样才不会丢失根本原因。只有在特殊情况下,您发现可能出现问题且不受 Java 代码控制的情况下,您才应该捕获
Error
或Throwable
。Throwable
is super class ofException
as well asError
. In normal cases we should always catch sub-classes ofException
, so that the root cause doesn't get lost.Only special cases where you see possibility of things going wrong which is not in control of your Java code, you should catch
Error
orThrowable
.Throwable 捕获了所有内容,甚至包括默认抛出的 ThreadDeath,以通过现已弃用的 Thread.stop() 方法停止线程。因此,通过捕获 Throwable,您可以确保在不至少经过 catch 块的情况下永远不会离开 try 块,但您应该准备好处理 OutOfMemoryError 和
InternalError
或StackOverflowError
。捕获 Throwable 对于将各种请求委托给外部代码但其本身可能永远不会终止以保持服务活动的外部服务器循环最有用。
Throwable
catches really everything even ThreadDeath which gets thrown by default to stop a thread from the now deprecatedThread.stop()
method. So by catchingThrowable
you can be sure that you'll never leave the try block without at least going through your catch block, but you should be prepared to also handleOutOfMemoryError
andInternalError
orStackOverflowError
.Catching
Throwable
is most useful for outer server loops that delegate all sorts of requests to outside code but may itself never terminate to keep the service alive.我见过人们使用 Throwable 来捕获由于基础设施故障/不可用而可能发生的一些错误。
I have seen people use Throwable to catch some errors that might happen due to infra failure/ non availability.