![](/img/trans.png)
[英]SonarQube error: Refactor this method to throw at most one checked exception
[英]Refactor method to throw at most one checked exception instead of ExecutionException and InterruptedException
我有一个类似的方法
public void methodName() throws ExecutionException, InterruptedException {}
SonarQube在此方法上提出了一个问题,建议重构此代码。
如果我将这些异常替换为Exception
(它们都扩展了),则表示抛出Exception
太笼统了。
我该如何解决这个问题?
确切的sonarQube消息:重构此方法以引发至少一个检查的异常,而不是ExecutionException,InterruptedException
sonarQube的详细提示: https ://sbforge.org/sonar/rules/show/squid:S1160 ? layout = false
我真的没有答案,但是我想也许有必要更深入地了解“例外”。 我在docs.oracle.com上查找了以下定义:
ExecutionException:尝试检索因引发异常而中止的任务的结果时引发的异常。 可以使用Throwable.getCause()方法检查此异常。
InteruptedException:在活动之前或活动期间,线程在等待,睡眠或以其他方式占用并且线程被中断时抛出。 有时,一种方法可能希望测试当前线程是否已被中断,如果已中断,则立即抛出该异常。 以下代码可用于实现此效果:if(Thread.interrupted())//清除中断状态! 抛出新的InterruptedException();
话虽这么说,也许ExecutionException的性质意味着我们不必担心捕获InteruptedException(因为在错误处理另一个抛出的异常时会抛出ExcecutionException)。 所以也许我们尝试这样的事情:
public void methodName() throws ExecutionException {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.