@PostConstruct & Checked exceptions

fasseg picture fasseg · Jan 5, 2012 · Viewed 21.2k times · Source

In the @PostConstruct doc it says about the annotated methods:

"The method MUST NOT throw a checked exception."

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?

Answer

Bozho picture Bozho · Jan 5, 2012

Yes, wrap it in a runtime exception. Preferebly something more concrete like IllegalStateException.

Note that if the init method fails, normally the application won't start.