本文介绍了捕获 Throwable 是一种不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

捕获 Throwable 是一种不好的做法吗?

Is it a bad practice to catch Throwable?

例如像这样:

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.

此外,Throwable 涵盖 Error 以及通常没有回报.您不想捕获/处理它,您希望您的程序立即终止,以便您可以正确修复它.

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.

这篇关于捕获 Throwable 是一种不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 20:57