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

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

问题描述

我们已经从软件供应商那里收到了Java代码。它包含很多 try-catch 块,在 catch 部分中没有。他们都在这个地方。示例:

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

我的问题是:是以上可接受的做法?如果是,什么时候?或者我应该继续删除所有这些虚假尝试 catch 语句?对于我来说,这看起来像是可怕的做法,但是我没有足够的经验来告诉Java。为什么不跟他们做任何事情来抓错?似乎对我来说,只有当你确信一个例外是绝对没有后果的时候,你才会做到这一点,而且不关心是否出现这种情况。但是,在我们的特定应用中并非如此。



编辑给出一些上下文:我们从供应商那里购买了一个Java脚本的产品。除了产品外,他们提供了一个适合我们需求的大型概念脚本。这个脚本是免费的(虽然我们不会购买产品,如果没有附带脚本),它工作。但是脚本是一个真正的痛苦,因为许多事情,即使我作为一个Java新手认识到可怕的做法,一个例子是这个虚假的try-catch业务。

解决方案

这确实是可怕的做法。特别是捕捉异常而不是特定的东西发出可怕的气味 - 即使一个 NullPointerException 将被吞噬。即使放心,特定抛出的异常没有真正的后果,应该始终至少记录:

 尝试{
// code
}
catch(MyInconsequentialException mie){
//在记录配置文件中这个记录器的调整级别,如果这太垃圾
MY_LOGGER。警告(抓起一个无关紧要的例外,三重);
}

但是在这种情况下,异常是不可能完全没有意义的。我建议研究究竟应用程序的代码是否打算在这里吞下什么异常,以及它们对执行意味着什么。



一个重要的区别是是否尝试/ catch用于吞咽检查异常。如果是这样的话,这可能表明程序员很冷漠 - 有人只是想让他/她的代码进行编译。至少应该修改代码:

  try {
// code
}
catch(SpecificCheckedException sce){
//确保有异常日志记录完成更多
抛出新的RuntimeException(sce);
}

这将重新抛出未经检查的 RuntimeException中包含的异常,有效地允许代码编译。即使这可以被认为是一个带状,但是,最佳实践是检查异常是以个别方式处理它们,无论是在当前方法中还是通过添加将SpecificCheckedException 抛出到方法签名。



作为 @Tom Hawtin 可以使用新的错误(sce)而不是新的RuntimeException(sce),以规避任何额外的异常越来越多,这对于预期不会被抛出的东西是有意义的。



如果尝试/ 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天全站免登陆