抓住可怜的行为是不好的做法吗? [英] Is it a bad practice to catch Throwable?

查看:126
本文介绍了抓住可怜的行为是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如这样的东西:



  try {
//某些代码
} catch(Throwable e){
//处理异常
}

这是不好的做法还是我们应该尽可能的具体? p>

解决方案

你需要尽可能的具体。否则不可预见的错误可能会以这种方式消失。



此外, Throwable 涵盖 错误 以及通常没有任何回报点。你不想捕捉/处理,你希望你的程序立即死亡,以便您可以正确地修复它。


Is it a bad practice to catch Throwable?

For example something like this:

try {
    // Some code
} catch(Throwable e) {
    // handle the exception
}

Is this a bad practice or we should be as specific as possible?

解决方案

You need to be as specific as possible. Otherwise unforeseen bugs might creep away this way.

Besides, Throwable covers Error as well and that's usually no point of return. You don't want to catch/handle that, you want your program to die immediately so that you can fix it properly.

这篇关于抓住可怜的行为是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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