在 try catch 中使用 Throwable 和 Exception 的区别 [英] Difference between using Throwable and Exception in a try catch

查看:48
本文介绍了在 try catch 中使用 Throwable 和 Exception 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,我看到

try {

} catch(Throwable e) {

}

有时

try {

} catch(Exception e) {

}

有什么区别?

推荐答案

通过捕获 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.

这篇关于在 try catch 中使用 Throwable 和 Exception 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆