I have created custom base exception that extends Exception
class. Later on I'm extending MyBaseException
class by another - this time specyfic exception class. And I now have problem with one of SonarQube - to be specyfic with "Classes named like "Exception" should extend "Exception" or a subclass" rule. Class declarations looks like that:
import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
public class MyBaseException extends Exception {
public MyBaseException(int code, String message) {
super(message);
this.code = code;
LOGGER.error("Exception with HttpStatus code: " + code + " Msg: "
+ message);
}
public MyBaseException(HttpStatus code, String message) {
this(code.value(), message);
}
public int getCode() {
return code;
}
}
import org.springframework.http.HttpStatus;
public class SpecException extends MyBaseException {
public SpecException (HttpStatus code, String message) {
super(code, message);
}
public SpecException (int code, String message) {
super(code, message);
}
}
Problem is only with this SpecException
. SonarQube don't see any issues with first inheritance. Hint given by SonarQube is: "Rename this class to remove "Exception" or correct its inheritance." but I think inheritance is correct since I'm inheriting by subclass of Exception
.
In simple words, we can say that a User-Defined Exception or custom exception is creating your own exception class and throwing that exception using the 'throw' keyword. For example, MyException in the below code extends the Exception class.
In Python, users can define custom exceptions by creating a new class. This exception class has to be derived, either directly or indirectly, from the built-in Exception class. Most of the built-in exceptions are also derived from this class.
At this link someone says that Sonar 6.0 corrects this issue: https://groups.google.com/forum/#!topic/sonarqube/iXxKGIYRZ1g.
I have just realised Sonar v6.0 is out, the problem is gone after I tested with the new version. Thank you Michael for your time.
It seems like it is a bug on previous versions of Sonar. It's well known that all exception classes in Java are named by convention ending with "Exception" and that they can have multiple levels so your code seems to be correct as soon as your upper level exception is extending the Exception
class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With