Try-catch:这是可以接受的做法吗? [英] Try-catch: is this acceptable practice?

查看:22
本文介绍了Try-catch:这是可以接受的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们收到了来自软件供应商的 Java 代码.它包含许多 try-catch 块,catch 部分没有任何内容.他们到处都是.示例:

 试试 {spaceBlock.enable(LindsayModel);} 捕获(异常 e){}

我的问题是:上述做法是否可以接受?如果有,是什么时候?或者我应该继续删除所有这些虚假"trycatch 语句?

对我来说,这看起来很糟糕,但我在 Java 方面经验不足,无法确定.如果你不打算对它们做任何事情,为什么要捕捉错误呢?在我看来,只有当您确信异常绝对没有后果并且您不在乎是否发生异常时,您才会这样做.然而,在我们的特定应用中,情况并非如此.

编辑 提供一些背景信息:我们从供应商那里购买了一个可编写 Java 脚本的产品.除了产品之外,他们还提供了一个根据我们的需求量身定制的大型概念验证脚本.这个脚本是免费的"(尽管如果没有脚本,我们就不会购买该产品)并且它有效".但是脚本确实很难构建,因为许多事情即使我作为 Java 新手也认为是糟糕的做法,其中一个例子是这种虚假的尝试捕获业务.

解决方案

这确实是很糟糕的做法.尤其是捕获Exception 而不是特定的东西会散发出可怕的气味——即使是NullPointerException 也会被吞下.即使可以确保特定抛出的异常没有实际后果,但至少应该始终将其记录下来:

尝试{//代码}赶上(MyInconsequentialException mie){//如果这太垃圾邮件,请在日志配置文件中调整此记录器的级别MY_LOGGER.warning("发现一个无关紧要的异常.", mie);}

然而,在这种情况下,异常不太可能完全没有意义.我建议仔细研究应用程序的代码打算在这里吞下哪些异常,以及它们对执行的真正含义.

一个重要的区别是 try/catch 是否用于吞下已检查的异常.如果是这种情况,这可能表明程序员极端冷漠——有人只是想让他/她的代码被编译.至少,应该修改代码:

尝试{//代码}捕捉(SpecificCheckedException sce){//确保有更远的异常日志记录抛出新的运行时异常(sce);}

这将重新抛出包装在未经检查的 RuntimeException 中的异常,从而有效地允许代码编译.然而,即使这也可以被视为创可贴 - 已检查异常的最佳实践是单独处理它们,无论是在当前方法中还是通过将 throws SpecificCheckedException 添加到方法签名.

正如 @Tom Hawtin 所提到的,new Error(sce) 可以被用来代替 new RuntimeException(sce) 以规避任何额外的 Exception 追赶更远,这对于不希望抛出的东西是有意义的.>

如果 try/catch 没有被用来吞下检查过的异常,它同样危险,应该简单地删除.

We have received Java code from a software supplier. It contains a lot of try-catch blocks with nothing in the catch part. They're all over the place. Example:

        try {
            spaceBlock.enable(LindsayModel);
        } catch (Exception e) {
        }

My questions are: Is the above acceptable practice? If so, when? Or should I just go ahead and remove all of these "bogus" try and catch statements?

To me this looks like terrible practice, but I'm not experienced enough in Java to tell for sure. Why catch errors if you're not going to do anything with them? Seems to me, you would only do that if you were confident that an exception would be of absolutely no consequence and you don't care if one occurs. However, this is not really the case in our particular application.

EDIT To give some context: We bought a Java-scriptable product from the supplier. Alongside the product, they provided a large proof-of-concept script tailored to our needs. This script came "free of charge" (though we wouldn't have bought the product if it hadn't come with the script) and it "works". But the script is a real pain to build upon, due to many things that even I as a Java novice recognise as awful practice, one instance being this bogus try-catch business.

解决方案

This is indeed terrible practice. Especially the catching of Exception rather than something specific gives off a horrible smell - even a NullPointerException will be swallowed. Even if it is assured that a particular thrown exception is of no real consequence, one should always log it at the very least:

try {
    // code
}
catch (MyInconsequentialException mie) {
   // tune level for this logger in logging config file if this is too spammy
   MY_LOGGER.warning("Caught an inconsequential exception.", mie);
}

However it is unlikely an exception is completely meaningless in this situation. I recommend researching exactly what exception(s) the application's code is intending to swallow here, and what they would really mean for the execution.

One important distinction is whether the try/catches are used to swallow checked exceptions. If this is the case, it probably indicates extreme apathy on the programmer's part - somebody just wanted his/her code to compile. At the least, the code should be amended:

try {
   // code
}
catch (SpecificCheckedException sce) {
   // make sure there is exception logging done farther up
   throw new RuntimeException(sce);
}

This will rethrow the exception wrapped in an unchecked RuntimeException, effectively allowing the code to compile. Even this can be considered a bandaid however - best practice for checked exceptions is to handle them on an individual basis, either in the current method or farther up by adding throws SpecificCheckedException to the method signature.

As @Tom Hawtin mentioned, new Error(sce) can be used instead of new RuntimeException(sce) in order to circumvent any additional Exception catches farther up, which makes sense for something that isn't expected to be thrown.

If the try/catch is not being used to swallow checked exceptions, it is equally dangerous and should simply be removed.

这篇关于Try-catch:这是可以接受的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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