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
Difference between using Throwable and Exception in a try catch
提问by jax
Sometimes, I see
有时,我看到
try {
} catch(Throwable e) {
}
And sometimes
而有时
try {
} catch(Exception e) {
}
What is the difference?
有什么不同?
采纳答案by Yishai
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 anythingthat goes wrong with that code, as much as possible.
通过捕获Throwable
它包括子类的东西Error
。您通常不应该这样做,除非在线程的最高“捕获所有”级别,您想记录或以其他方式处理所有可能出错的事情。在框架类型的应用程序(例如应用程序服务器或测试框架)中更典型,它可以运行未知代码,并且尽可能不受该代码出错的任何影响。
回答by BalusC
The first one catches all subclasses of Throwable
(this includes Exception
and Error
), the second one catches all subclasses of Exception
.
第一个捕获Throwable
(这包括Exception
和Error
)的所有子类,第二个捕获 的所有子类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.
Error
以任何方式以编程方式无法恢复并且通常不会被捕获,除非出于记录目的(再次通过它)。Exception
可以通过编程方式恢复。它的子类RuntimeException
表示编程错误,通常也不会被捕获。
回答by x4u
Thowable
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
.
Thowable
捕捉真正的一切,甚至是 ThreadDeath,默认情况下它会被抛出以从现在已弃用的Thread.stop()
方法中停止线程。因此,通过捕获,Throwable
您可以确保至少在不经过 catch 块的情况下永远不会离开 try 块,但是您应该准备好同时处理OutOfMemoryError
andInternalError
或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.
捕获Throwable
对于将各种请求委托给外部代码但本身可能永远不会终止以保持服务活动的外部服务器循环最有用。
回答by rai.skumar
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.
Throwable
是Exception
以及 的超类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 Error
or Throwable
.
只有在您看到 Java 代码无法控制的出错可能性的特殊情况下,您才应该 catchError
或Throwable
.
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 来捕获一些可能由于基础设施故障/不可用而发生的错误。