在try catch中使用Throwable和Exception之间的区别 [英] Difference between using Throwable and Exception in a try catch
问题描述
有时我会看到
try {
} catch(Throwable e){
}
有时
try {
pre>
} catch(Exception e){
}
有什么区别
解决方案通过捕捉Throwable它包括子类错误的东西。你通常不应该这样做,除非是在你想要登录或以其他方式处理可能出错的一切的线程的最高catch all级别。在框架类型应用程序(例如应用程序服务器或测试框架)中,它可能会运行未知代码并且不应受到该代码出错的任何的影响,将会更为典型,例如尽可能的。
Sometimes I see
try { } catch(Throwable e) { }
And sometimes
try { } catch(Exception e) { }
What is the difference
解决方案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.
这篇关于在try catch中使用Throwable和Exception之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!