如果模块没有注入(inject),在methodA中我可以抛出哪种类型的异常?
public class Customer{
private String module;
public void methodA(){
if (StringUtils.isBlank(module))
throw new ???
}
}
最佳答案
您可以创建自己的自定义异常,或者抛出 IllegalStateException
以及适当的消息。来自文档:
Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation. (emphasis mine)
由于您不希望 module
为空,因此您处于无效状态,因此在这种情况下(IMO)抛出此异常是合适的。
关于java - 如果未注入(inject)属性,抛出什么类型的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31483353/