在 try catch 中使用 Throwable 和 Exception 之间的区别

发布于 2024-08-21 22:34:43 字数 158 浏览 8 评论 0原文

有时,我看到

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

野味少女 2024-08-28 22:34:43

通过捕获 Throwable,它包含了 Error 子类的内容。通常,您不应该这样做,除非在线程的最高“捕获所有”级别,您想要记录或以其他方式处理绝对可能出错的所有内容。它在框架类型应用程序(例如应用程序服务器或测试框架)中更为典型,其中可以运行未知代码,并且不应受到该代码出现问题的任何影响,如下所示尽可能多。

By catching Throwable it includes things that subclass Error. 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.

待天淡蓝洁白时 2024-08-28 22:34:43

第一个捕获 Throwable (这包括 异常Error),第二个捕获 异常

Error 在编程上是无法以任何方式恢复的,并且通常不会被捕获,除非用于日志记录目的(再次传递它)。 Exception 可以通过编程方式恢复。它的子类RuntimeException表示编程错误,通常也不会被捕获。

The first one catches all subclasses of Throwable (this includes Exception and Error), the second one catches all subclasses of Exception.

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 subclass RuntimeException indicates a programming error and is usually not to be caught as well.

慕烟庭风 2024-08-28 22:34:43

我觉得这应该在这里:

Java Exception Hierarchy Image

( img 的 Alt 链接

来源:

呆橘 2024-08-28 22:34:43

ThrowableException 以及Error 的超类。在正常情况下,我们应该始终捕获Exception的子类,这样才不会丢失根本原因。

只有在特殊情况下,您发现可能出现问题且不受 Java 代码控制的情况下,您才应该捕获 ErrorThrowable

我记得捕获 Throwable 来标记未加载本机库。

Throwable is super class of Exception as well as Error. In normal cases we should always catch sub-classes of Exception, 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 or Throwable.

I remember catching Throwable to flag that a native library is not loaded.

寄意 2024-08-28 22:34:43

Throwable 捕获了所有内容,甚至包括默认抛出的 ThreadDeath,以通过现已弃用的 Thread.stop() 方法停止线程。因此,通过捕获 Throwable,您可以确保在不至少经过 catch 块的情况下永远不会离开 try 块,但您应该准备好处理 OutOfMemoryError 和InternalErrorStackOverflowError

捕获 Throwable 对于将各种请求委托给外部代码但其本身可能永远不会终止以保持服务活动的外部服务器循环最有用。

Throwable catches really everything even ThreadDeath which gets thrown by default to stop a thread from the now deprecated Thread.stop() method. So by catching Throwable 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 handle OutOfMemoryError and InternalError or StackOverflowError.

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.

A君 2024-08-28 22:34:43

我见过人们使用 Throwable 来捕获由于基础设施故障/不可用而可能发生的一些错误。

I have seen people use Throwable to catch some errors that might happen due to infra failure/ non availability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文