@PostConstruct &检查异常 [英] @PostConstruct & Checked exceptions
问题描述
在 @PostConstruct 文档中它说注释方法:
In the @PostConstruct doc it says about the annotated methods:
该方法不得抛出已检查的异常."
人们将如何处理例如可以在这种方法中抛出的 IOException?只是将它包装在一个 RuntimeException 中,让用户担心对象的错误初始状态?或者@PostConstruct 是验证和初始化注入依赖项的对象的错误位置?
How would one deal with e.g. an IOException which can be thrown in such a method? Just wrap it in a RuntimeException and let the user worry about the faulty initial state of the object? Or is @PostConstruct the wrong place to validate and initialize objects which got their dependencies injected?
推荐答案
是的,将其包装在运行时异常中.最好是更具体的东西,比如 IllegalStateException
.
Yes, wrap it in a runtime exception. Preferebly something more concrete like IllegalStateException
.
请注意,如果 init 方法失败,则应用程序通常不会启动.
Note that if the init method fails, normally the application won't start.
这篇关于@PostConstruct &检查异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!