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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2274102/
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-08-13 05:33:12  来源:igfitidea点击:

Difference between using Throwable and Exception in a try catch

javaexceptionthrowable

提问by jax

Sometimes, I see

有时,我看到

try {

} catch(Throwable e) {

}

And sometimes

而有时

try {

} catch(Exception e) {

}

What is the difference?

有什么不同?

采纳答案by Yishai

By catching Throwableit 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 anythingthat goes wrong with that code, as much as possible.

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

回答by BalusC

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

第一个捕获Throwable(这包括ExceptionError)的所有子类,第二个捕获 的所有子类Exception

Erroris programmatically unrecoverable in any way and is usually not to be caught, except for logging purposes (which passes it through again). Exceptionis programmatically recoverable. Its subclass RuntimeExceptionindicates a programming error and is usually not to be caught as well.

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

回答by x4u

Thowablecatches really everything even ThreadDeath which gets thrown by default to stop a thread from the now deprecated Thread.stop()method. So by catching Throwableyou 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 OutOfMemoryErrorand InternalErroror StackOverflowError.

Thowable捕捉真正的一切,甚至是 ThreadDeath,默认情况下它会被抛出以从现在已弃用的Thread.stop()方法中停止线程。因此,通过捕获,Throwable您可以确保至少在不经过 catch 块的情况下永远不会离开 try 块,但是您应该准备好同时处理OutOfMemoryErrorandInternalErrorStackOverflowError

Catching Throwableis 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对于将各种请求委托给外部代码但本身可能永远不会终止以保持服务活动的外部服务器循环最有用。

回答by rai.skumar

Throwableis super class of Exceptionas well as Error. In normal cases we should always catch sub-classes of Exception, so that the root cause doesn't get lost.

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

Only special cases where you see possibility of things going wrong which is not in control of your Java code, you should catch Erroror Throwable.

只有在您看到 Java 代码无法控制的出错可能性的特殊情况下,您才应该 catchErrorThrowable.

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

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

回答by Spear A1

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

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