java - 如果未注入(inject)属性,抛出什么类型的异常?

标签 java spring exception

如果模块没有注入(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/

相关文章:

JavaFX 绑定(bind)/循环问题

java - 在 Linux 操作系统上使用正确的 JDK 或更改 Netbeans 中的默认值

java - 没有 AlgorithmParameterSpec 可以使用 KeyAgreement 吗?

spring - 无法启动嵌入式 Tomcat NoClassDefFoundError DispatcherServletPath

Java Spring 和 CORS

ruby - 有没有其他方法可以为 Ruby 挽救 LoadError?

java - 在 JFrame 中显示图像

java - NoSuchMethodError :registerAutoProxyCreatorIfNecessary(ParserContext;Object;) exception with Spring config 错误

c++ - 交换基本类型的对象会抛出异常吗?

php - 异常困惑