简体   繁体   English

重构方法以引发最多一个检查的异常,而不是ExecutionException和InterruptedException

[英]Refactor method to throw at most one checked exception instead of ExecutionException and InterruptedException

I have a method like 我有一个类似的方法

public void methodName() throws ExecutionException, InterruptedException {}

SonarQube raises an issue on this method, suggesting to refactor this code. SonarQube在此方法上提出了一个问题,建议重构此代码。

If I replace those exceptions with Exception (which both of them extend), then it says throwing Exception is too generic. 如果我将这些异常替换为Exception (它们都扩展了),则表示抛出Exception太笼统了。

How can I resolve this issue? 我该如何解决这个问题?

Exact sonarQube message: Refactor this method to throw atmost one checked exception instead of ExecutionException , InterruptedException 确切的sonarQube消息:重构此方法以引发至少一个检查的异常,而不是ExecutionException,InterruptedException

Detailed Hint by sonarQube: https://sbforge.org/sonar/rules/show/squid:S1160?layout=false sonarQube的详细提示: https ://sbforge.org/sonar/rules/show/squid:S1160 ? layout = false

I don't really have the answer for you, but I guess it might be worth looking more in depth at the Exceptions. 我真的没有答案,但是我想也许有必要更深入地了解“例外”。 I looked up the following definitions on docs.oracle.com: 我在docs.oracle.com上查找了以下定义:

ExecutionException: Exception thrown when attempting to retrieve the result of a task that aborted by throwing an exception. ExecutionException:尝试检索因引发异常而中止的任务的结果时引发的异常。 This exception can be inspected using the Throwable.getCause() method. 可以使用Throwable.getCause()方法检查此异常。

InteruptedException: Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. InteruptedException:在活动之前或活动期间,线程在等待,睡眠或以其他方式占用并且线程被中断时抛出。 Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception. 有时,一种方法可能希望测试当前线程是否已被中断,如果已中断,则立即抛出该异常。 The following code can be used to achieve this effect: if (Thread.interrupted()) // Clears interrupted status! 以下代码可用于实现此效果:if(Thread.interrupted())//清除中断状态! throw new InterruptedException(); 抛出新的InterruptedException();

That being said, maybe the nature of the ExecutionException means that we don't have to worry about catching the InteruptedException (since an ExcecutionException is thrown when mishandling another thrown exception). 话虽这么说,也许ExecutionException的性质意味着我们不必担心捕获InteruptedException(因为在错误处理另一个抛出的异常时会抛出ExcecutionException)。 So maybe we try something like this: 所以也许我们尝试这样的事情:

public void methodName() throws ExecutionException {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SonarQube错误:重构此方法最多抛出一个已检查的异常 - SonarQube error: Refactor this method to throw at most one checked exception 为什么InterruptedException是一个经过检查的异常? - Why Is InterruptedException a Checked Exception? 在什么情况下 Future.get() 会抛出 ExecutionException 或 InterruptedException - In what cases does Future.get() throw ExecutionException or InterruptedException 抛出新的InterruptedException()代替.join() - Throw new InterruptedException() instead .join() 在java中将已检查的异常抛出为未检查而不是包装 - throw checked exception as unchecked in java instead of wrapping “专门化”一个子类型的方法来抛出一个已检查的异常? - “Specializing” a subtype's method to throw a checked exception? 什么时候应该抛出InterruptedException,我应该如何处理呢? (阻塞方法) - When should a method throw InterruptedException, and how should I handle one that does? (blocking method) 当方法中没有代码会抛出检查异常时声明“抛出异常” - Declaring 'throws Exception' when no code in a method would throw checked exception 抛出异常而不是在Java方法中返回 - Throw exception instead of return in Java method 抛出一个检查异常 - Throw a checked exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM