I have a custom exception class written as
public class MyCustomException extends RuntimeException {
public MyCustomException(Throwable cause) {
super(cause);
}
enter code here
/**
* @param message
* @param cause
*/
public MyCustomException(String message, Throwable cause) {
super(message, cause);
}
}
In my serviceImpl layer
@Override
public List<SiteDetails> getSite() throws MyCustomException {
}
SONAR (Ecplise IDE plugin for linting) states:
Remove the declaration of thrown exception '.MyCustomException' which is a runtime exception
Should I remove the declaration or should I extend Exception
in MyCustomException
class instead of RunTimeException
?
The sonar issue is very very primarily opinion based.
A RuntimeException
is not forced to be handled and so declared but it is not a bad practice to declare it in a method to document this case.
Should I remove the declaration or should I extend Exception in MyCustomException class instead of RuntimeException
If you deem that MyCustomException
is an exception that has to be necessarily handled by the client, MyCustomException
should derive from Exception
rather thanRuntimeException
.
Otherwise if the exception cannot be handled by the client in most of circumstances, you should remove the declaration to make Sonar Happy or mark (at least trying) this issue as a false positive if you think that it makes sense to convey the exception even if it is a RuntimeException
.