2

I'm having a difficult time wrapping my head around the Javadoc for @PostConstruct. It says:

If the method throws an unchecked exception the class MUST NOT be put into service except in the case of EJBs where the EJB can handle exceptions and even recover from them

In prior versions of Java the docs read:

The method MUST NOT throw a checked exception

The latter requirement seems to have been dropped. What does it mean "If the method throws an unchecked exception the class MUST NOT be put into service"? Should I prefer checked exceptions to unchecked exceptions in this context or does it matter?

Note that I'm aware of this similar question, but the Javadoc has since changed and my lack of understanding around the unchecked scenario is what drew me here.

The Gilbert Arenas Dagger
  • 12,071
  • 13
  • 66
  • 80

1 Answers1

0

What does it mean "If the method throws an unchecked exception the class MUST NOT be put into service"?

The exact behaviour depends on the container managing the bean.

An instance of CDI bean will be thrown away and not used which usually leads to unsatisfied dependency. Unfortunately, I could not find any reference.

An instance of EJB will be thrown away and container will rollback any container-started transaction. Check section "9.3.3 Exceptions from AroundConstruct, PostConstruct and PreDestroy Lifecycle Callbacks" of EJB 3.2 spec.

Should I prefer checked exceptions to unchecked exceptions in this context or does it matter?

@PostConstruct javadoc prohibits any checked exceptions to be thrown from the body of annotated method.

Illya Kysil
  • 1,642
  • 10
  • 18