简体   繁体   English

SonarQube错误:重构此方法最多抛出一个已检查的异常

[英]SonarQube error: Refactor this method to throw at most one checked exception

I am using SonarQube and it shows the following error: 我正在使用SonarQube,它显示以下错误:

Public methods should throw at most one checked exception.

// Noncompliant
public void delete() throws IOException, SQLException { /* ... */ }

// Compliant
public void delete() throws SomeApplicationLevelException { /* ... */ }

Does this means, SomeApplicationLevelException is a parent class and IOException and SQALException are derived from it? 这是否意味着, SomeApplicationLevelException是一个父类, IOExceptionSQALException是从它派生的? And we should throw the parent class exception? 我们应该抛出父类异常? Thereby adhering to method throwing only 1 checked exception? 从而坚持只抛出1个检查异常的方法?

Because I have 2 exceptions that i have defined say for example Exception1 and Exception2 that extend Exception . 因为我有2个例外,我已经定义了例如Exception1Exception2来扩展Exception And my method say, sampleMethod() throws them ie, 我的方法说, sampleMethod()抛出它们,即

public void sampleMethod() throws Exception1, Exception2 {
}

And the error is shown here. 这里显示错误。 So should I have one class as parent (say MainException ) and derive Exception1 and Exception2 from it and throw parent exception class? 那么我应该将一个类作为父类(比如MainException )并从中派生Exception1Exception2并抛出父异常类吗? Like below: 如下所示:

public void sampleMethod() throws MainException {
}

Is the above solution proper? 以上解决方案是否合适?

If you have a method in your application that is declared as throws SQLException, IOException , you are probably leaking internal implementation details to your method's users. 如果您的应用程序中有一个声明为throws SQLException, IOException ,您可能会将内部实现细节泄露给方法的用户。 Specifically, you're saying: 具体来说,你说:

  1. Your method is implemented using JDBC and file I/O. 您的方法是使用JDBC和文件I / O实现的。 Your users don't care how your method is implemented; 您的用户不关心您的方法是如何实现的; they only care about what your method does. 他们只关心你的方法做什么。

  2. Your method, including any future version of it , will never throw any other checked exception. 您的方法, 包括它的任何未来版本 ,将永远不会抛出任何其他已检查的异常。 If in future you change your method so that it might throw another checked exception, it will break backwards compatibility. 如果将来更改方法以便它可能抛出另一个已检查的异常,它将破坏向后兼容性。

The advice is to create your own application-specific class (derived from Exception), and only throw that in your method. 建议是创建自己的特定于应用程序的类(派生自Exception),并且只在您的方法中抛出它。 You can, if you like, wrap the SQLException or the IOException (or any other exception) inside your application-specific exception as the cause . 如果您愿意,可以将特定于应用程序的异常中的SQLException或IOException(或任何其他异常)包装为原因

Note, however, that there is a school of thought that says Java checked exceptions are a bad idea (and one of the reasons C#, and more modern languages such as Kotlin, don't have checked exceptions). 但请注意,有一种思想流派认为Java检查异常是一个坏主意 (C#的原因之一,以及更多现代语言,如Kotlin,没有检查异常)。

UPDATE: the above answer related to the first version of the question (edit #1). 更新:上述答案与问题的第一个版本有关(编辑#1)。 The question was subsequently updated to point out that the two thrown exceptions were application-defined ones, so therefore much of the above rationale no longer applies. 随后更新了这个问题,指出两个抛出的异常是应用程序定义的异常,因此上述大部分理由不再适用。 The answer to the updated question is explained in this post . 答案将更新问题中解释这个职位

IOexception and sqlexception both are checked exception s,totally different from each other , now if we extend both from one exception and throw the parent exception , which is not a mandatory in java, it will be kind of misguiding the user of the api. IOexception和sqlexception都是经过检查的异常,彼此完全不同,现在如果我们从一个异常扩展并抛出父异常(这在java中不是必需的),它将会误导api的用户。

However, if you want to do it in ur app to avoid sonarqube error , you can catch all your specific exceptions and throw a custom exception wrapping the original exception information in exception message. 但是,如果你想在你的应用程序中这样做以避免sonarqube错误,你可以捕获所有特定的异常并抛出一个自定义异常包装异常消息中的原始异常信息。

for example 例如

try{

      ///piece of code that throws IOException and SQLException
}catch(IOException | SQLException ex){
   throw new DataException(ex,"Any customized message you want");
}

This DataException will then will be included in the throws clause of method signature having this try catch. 然后,此DataException将包含在具有此try catch的方法签名的throws子句中。

DataException extends Exception class and by passing ex in the contructor you are wrapping the original exception in custom exception with your original exception info preserved. DataException扩展了Exception类,并通过在构造函数中传递ex,将原始异常包装在自定义异常中,并保留原始异常信息。

暂无
暂无

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

相关问题 重构方法以引发最多一个检查的异常,而不是ExecutionException和InterruptedException - Refactor method to throw at most one checked exception instead of ExecutionException and InterruptedException 如果方法无法引发声明的已检查异常,则配置Java编译器是否出错 - Configure Java Compiler for Error if method cannot throw declared checked Exception 如何在不违反SonarQube的前提下引发检查异常? - How can you throw a checked exception without violating SonarQube? “专门化”一个子类型的方法来抛出一个已检查的异常? - “Specializing” a subtype's method to throw a checked exception? 尝试抛出已检查异常时出现编译器错误 - Compiler error when trying to throw a checked exception jaxws 和 apache cxf 和 glassfish 5 错误:java.lang.IllegalStateException:生命周期方法 [finalizeConfig] 不能抛出已检查的异常 - jaxws and apache cxf and glassfish 5 error: java.lang.IllegalStateException: The lifecycle method [finalizeConfig] must not throw a checked exception 当方法中没有代码会抛出检查异常时声明“抛出异常” - Declaring 'throws Exception' when no code in a method would throw checked exception 抛出一个检查异常 - Throw a checked exception Java Thread:Run方法不能抛出已检查的异常 - Java Thread: Run method cannot throw checked exception 使用 Java 可选 ifPresentOrElse 方法时无法抛出 `checked` 异常 - Unable to throw a `checked` exception when using Java Optional ifPresentOrElse method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM