我有一个自定义异常类,写为
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);
}
}
在我的serviceImpl层
@Override
public List<SiteDetails> getSite() throws MyCustomException {
}
SONAR(用于 linting 的 Ecplise IDE 插件)状态:
Remove the declaration of thrown exception '.MyCustomException' which is a runtime exception
我应该删除该声明还是应该在 MyCustomException
类中扩展 Exception
而不是 RunTimeException
?
最佳答案
声纳问题主要基于意见。
RuntimeException
不会被强制处理和声明,但在记录这种情况的方法中声明它并不是一个坏习惯。
Should I remove the declaration or should I extend Exception in MyCustomException class instead of RuntimeException
如果您认为 MyCustomException
是必须由客户端处理的异常,则 MyCustomException
应该从 Exception
派生,而不是运行时异常
。
否则,如果在大多数情况下客户端无法处理异常,则您应该删除声明以使 Sonar 满意,或者如果您认为传达以下信息是有意义的,则应将此问题标记(至少尝试)为误报:异常,即使它是 RuntimeException
。
关于java - SONAR 扩展异常还是运行时异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42063797/