The javadoc for @PostConstruct is pretty clear that a method annotated with
@PostConstruct can not throw checked exceptions:

http://docs.oracle.com/javaee/6/api/javax/annotation/PostConstruct.html


On Thu, Feb 6, 2014 at 8:07 AM, Lars-Fredrik Smedberg <[email protected]>wrote:

> I was not able to @PostConstruct annotate a interceptor method that had a
> return type and threw a checked exception, I got the following exception at
> startup
>
> @PostConstruct
>  public Object postConstructMethod(InvocationContext ctx) throws
> Exception {
>
> [ERROR   ] @PostConstruct annotated method : postConstructMethod in class
> : shb.rinf.util.CacheInterceptor must return void type
> @PostConstruct annotated method : postConstructMethod in class :
> shb.rinf.util.CacheInterceptor must return void type
> [ERROR   ] An error occured while starting application context path :
> [/rsea]
>
> and when i just removed the return type
>
> @PostConstruct
> public void postConstructMethod(InvocationContext ctx) throws Exception {
>
> [ERROR   ] @PostConstruct annotated method : postConstructMethod in class
> : shb.rinf.util.CacheInterceptor can not throw any checked exception
> @PostConstruct annotated method : postConstructMethod in class :
> shb.rinf.util.CacheInterceptor can not throw any checked exception
> [ERROR   ] An error occured while starting application context path :
> [/rsea]
>
> ....
>
> My test does not run the code in the interceptor before the exception is
> thrown in the target class @PostConstruct and therefor nothing is
> caught....
>
> Did I miss out on something?
>
> Regards
> Fredrik
>
>
>
>
> On Thu, Feb 6, 2014 at 1:49 PM, Romain Manni-Bucau 
> <[email protected]>wrote:
>
>> My code catches the exception of @PostConstruct method of the bean so
>> i should be missing sthg
>> Romain Manni-Bucau
>> Twitter: @rmannibucau
>> Blog: http://rmannibucau.wordpress.com/
>> LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> Github: https://github.com/rmannibucau
>>
>>
>>
>> 2014-02-06 Lars-Fredrik Smedberg <[email protected]>:
>> > Hi
>> >
>> > The reason I didn't see any calls to the target method annotated
>> > @PostConstruct was that it always threw an exception (since I wanted to
>> try
>> > to catch that).
>> > When the target method annotated @PostConstruct does not throw an
>> exception
>> > I see a call to the interceptors @PostConstruct method...
>> >
>> > I was not however able to catch any exception thrown by the
>> @PostConstruct
>> > method as you show in your example (I also had to remove the checked
>> > exception and set void as a return type in the interceptor method to be
>> able
>> > to pass the verification)
>> >
>> > Did I misundertand you or is it not possible to catch exceptions thrown
>> in
>> > the @PostConstruct in the same was as exceptions thrown from a business
>> > method?
>> >
>> > Regards
>> > Fredrik
>> >
>> >
>> >
>> >
>> > On Thu, Feb 6, 2014 at 12:07 PM, Romain Manni-Bucau <
>> [email protected]>
>> > wrote:
>> >>
>> >> not using interceptor spec AFAIK but constructors shouldn't throw an
>> >> exception, it should be in a @PostConstruct:
>> >>
>> >> @CatchMe
>> >> @Interceptor
>> >> public class Inter {
>> >> @PostConstruct
>> >> public Object post(final InvocationContext ic) throws Exception {
>> >> System.out.println(">>> ok");
>> >> System.out.println(">>> ok");
>> >> System.out.println(">>> ok");
>> >> System.out.println(">>> ok");
>> >> try {
>> >> return ic.proceed();
>> >> } catch (Exception e) {
>> >> e.printStackTrace();
>> >> return null;
>> >> }
>> >> }
>> >> }
>> >>
>> >>
>> >> @CatchMe
>> >> public class Foo {
>> >> @PostConstruct
>> >> public void Foo() {
>> >> throw new RuntimeException();
>> >> }
>> >>
>> >> public void run() {}
>> >> }
>> >>
>> >> Romain Manni-Bucau
>> >> Twitter: @rmannibucau
>> >> Blog: http://rmannibucau.wordpress.com/
>> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> >> Github: https://github.com/rmannibucau
>> >>
>> >>
>> >>
>> >> 2014-02-06 Lars-Fredrik Smedberg <[email protected]>:
>> >> > so there is no need to call ic.proceed either then?
>> >> >
>> >> > Is there any way to intercept the exceptions thrown from the
>> >> > @PostConstruct
>> >> > method in the same way I can with business methods....?
>> >> >
>> >> > Regards
>> >> > Fredrik
>> >> >
>> >> >
>> >> > On Thu, Feb 6, 2014 at 11:59 AM, Romain Manni-Bucau
>> >> > <[email protected]>
>> >> > wrote:
>> >> >>
>> >> >> after the bean construction so it will likely not work
>> >> >> Romain Manni-Bucau
>> >> >> Twitter: @rmannibucau
>> >> >> Blog: http://rmannibucau.wordpress.com/
>> >> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> >> >> Github: https://github.com/rmannibucau
>> >> >>
>> >> >>
>> >> >>
>> >> >> 2014-02-06 Lars-Fredrik Smedberg <[email protected]>:
>> >> >> > Hi Romain
>> >> >> >
>> >> >> > Maybe I misunderstood something about the use of it... some
>> >> >> > questions:
>> >> >> >
>> >> >> > 1. When is the interceptor method called? Before the
>> @PostConstruct
>> >> >> > method
>> >> >> > of the target bean I assume?
>> >> >> > 2. Do I need to call ic.proceed() for the target been method to be
>> >> >> > called?
>> >> >> >
>> >> >> > I'm looking for a way to intercept any runtime exceptions thrown
>> by
>> >> >> > the
>> >> >> > @PostContstruct method of the target bean.
>> >> >> >
>> >> >> > Regards
>> >> >> > Fredrik
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> > On Thu, Feb 6, 2014 at 11:50 AM, Romain Manni-Bucau
>> >> >> > <[email protected]>
>> >> >> > wrote:
>> >> >> >>
>> >> >> >> seems it works for me:
>> >> >> >>
>> >> >> >> @CatchMe
>> >> >> >> @Interceptor
>> >> >> >> public class Inter {
>> >> >> >> @PostConstruct
>> >> >> >> public void post(final InvocationContext ic) {
>> >> >> >> System.out.println(">>> ok");
>> >> >> >> System.out.println(">>> ok");
>> >> >> >> System.out.println(">>> ok");
>> >> >> >> System.out.println(">>> ok");
>> >> >> >> }
>> >> >> >> }
>> >> >> >>
>> >> >> >>
>> >> >> >> @InterceptorBinding
>> >> >> >> @Target(ElementType.TYPE)
>> >> >> >> @Retention(RetentionPolicy.RUNTIME)
>> >> >> >> public @interface CatchMe {
>> >> >> >> }
>> >> >> >>
>> >> >> >>
>> >> >> >> Romain Manni-Bucau
>> >> >> >> Twitter: @rmannibucau
>> >> >> >> Blog: http://rmannibucau.wordpress.com/
>> >> >> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> >> >> >> Github: https://github.com/rmannibucau
>> >> >> >>
>> >> >> >>
>> >> >> >>
>> >> >> >> 2014-02-06 Lars-Fredrik Smedberg <[email protected]>:
>> >> >> >> > Hi Romain
>> >> >> >> >
>> >> >> >> > In the Interceptor I tried:
>> >> >> >> >
>> >> >> >> > @PostConstruct
>> >> >> >> > public void interceptPostConstructMethod(InvocationContext
>> ctx) {
>> >> >> >> >    ...
>> >> >> >> > }
>> >> >> >> >
>> >> >> >> >
>> >> >> >> > On Thu, Feb 6, 2014 at 11:08 AM, Romain Manni-Bucau
>> >> >> >> > <[email protected]>
>> >> >> >> > wrote:
>> >> >> >> >>
>> >> >> >> >> Hi
>> >> >> >> >>
>> >> >> >> >> what's the signature you used for your @PostContruct method?
>> >> >> >> >> Romain Manni-Bucau
>> >> >> >> >> Twitter: @rmannibucau
>> >> >> >> >> Blog: http://rmannibucau.wordpress.com/
>> >> >> >> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> >> >> >> >> Github: https://github.com/rmannibucau
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >> 2014-02-06 Lars-Fredrik Smedberg <[email protected]>:
>> >> >> >> >> > Hi
>> >> >> >> >> >
>> >> >> >> >> > When looking at
>> >> >> >> >> > http://docs.oracle.com/javaee/6/tutorial/doc/gkigq.html
>> >> >> >> >> > it
>> >> >> >> >> > looks like I could create an Interceptor with a method
>> >> >> >> >> > annotated
>> >> >> >> >> > @PostConstruct which would get called when the annotated
>> target
>> >> >> >> >> > class
>> >> >> >> >> > @PostConstruct  annoted method is called.
>> >> >> >> >> >
>> >> >> >> >> > I cannot get this to work, however an Interceptor having a
>> >> >> >> >> > method
>> >> >> >> >> > annotated
>> >> >> >> >> > @AroundInvoke works fine when calling business methods in
>> the
>> >> >> >> >> > annotated
>> >> >> >> >> > target class.
>> >> >> >> >> >
>> >> >> >> >> > Can anyone point me in the right direction to get it to work
>> >> >> >> >> > (or
>> >> >> >> >> > maybe
>> >> >> >> >> > tell
>> >> >> >> >> > it is not supposed to work as I think it should :))
>> >> >> >> >> >
>> >> >> >> >> > Regards
>> >> >> >> >> > Fredrik
>> >> >> >> >> >
>> >> >> >> >> > --
>> >> >> >> >> > Med vänlig hälsning / Best regards
>> >> >> >> >> >
>> >> >> >> >> > Lars-Fredrik Smedberg
>> >> >> >> >> >
>> >> >> >> >> > STATEMENT OF CONFIDENTIALITY:
>> >> >> >> >> > The information contained in this electronic message and any
>> >> >> >> >> > attachments to this message are intended for the exclusive
>> use
>> >> >> >> >> > of
>> >> >> >> >> > the
>> >> >> >> >> > address(es) and may contain confidential or privileged
>> >> >> >> >> > information.
>> >> >> >> >> > If
>> >> >> >> >> > you are not the intended recipient, please notify
>> Lars-Fredrik
>> >> >> >> >> > Smedberg
>> >> >> >> >> > immediately at [email protected], and destroy all copies
>> of
>> >> >> >> >> > this
>> >> >> >> >> > message and any attachments.
>> >> >> >> >
>> >> >> >> >
>> >> >> >> >
>> >> >> >> >
>> >> >> >> > --
>> >> >> >> > Med vänlig hälsning / Best regards
>> >> >> >> >
>> >> >> >> > Lars-Fredrik Smedberg
>> >> >> >> >
>> >> >> >> > STATEMENT OF CONFIDENTIALITY:
>> >> >> >> > The information contained in this electronic message and any
>> >> >> >> > attachments to this message are intended for the exclusive use
>> of
>> >> >> >> > the
>> >> >> >> > address(es) and may contain confidential or privileged
>> >> >> >> > information.
>> >> >> >> > If
>> >> >> >> > you are not the intended recipient, please notify Lars-Fredrik
>> >> >> >> > Smedberg
>> >> >> >> > immediately at [email protected], and destroy all copies of
>> this
>> >> >> >> > message and any attachments.
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> > --
>> >> >> > Med vänlig hälsning / Best regards
>> >> >> >
>> >> >> > Lars-Fredrik Smedberg
>> >> >> >
>> >> >> > STATEMENT OF CONFIDENTIALITY:
>> >> >> > The information contained in this electronic message and any
>> >> >> > attachments to this message are intended for the exclusive use of
>> the
>> >> >> > address(es) and may contain confidential or privileged
>> information.
>> >> >> > If
>> >> >> > you are not the intended recipient, please notify Lars-Fredrik
>> >> >> > Smedberg
>> >> >> > immediately at [email protected], and destroy all copies of this
>> >> >> > message and any attachments.
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> > Med vänlig hälsning / Best regards
>> >> >
>> >> > Lars-Fredrik Smedberg
>> >> >
>> >> > STATEMENT OF CONFIDENTIALITY:
>> >> > The information contained in this electronic message and any
>> >> > attachments to this message are intended for the exclusive use of the
>> >> > address(es) and may contain confidential or privileged information.
>> If
>> >> > you are not the intended recipient, please notify Lars-Fredrik
>> Smedberg
>> >> > immediately at [email protected], and destroy all copies of this
>> >> > message and any attachments.
>> >
>> >
>> >
>> >
>> > --
>> > Med vänlig hälsning / Best regards
>> >
>> > Lars-Fredrik Smedberg
>> >
>> > STATEMENT OF CONFIDENTIALITY:
>> > The information contained in this electronic message and any
>> > attachments to this message are intended for the exclusive use of the
>> > address(es) and may contain confidential or privileged information. If
>> > you are not the intended recipient, please notify Lars-Fredrik Smedberg
>> > immediately at [email protected], and destroy all copies of this
>> > message and any attachments.
>>
>
>
>
> --
> Med vänlig hälsning / Best regards
>
> Lars-Fredrik Smedberg
>
> STATEMENT OF CONFIDENTIALITY:
> The information contained in this electronic message and any
> attachments to this message are intended for the exclusive use of the
> address(es) and may contain confidential or privileged information. If
> you are not the intended recipient, please notify Lars-Fredrik Smedberg
> immediately at [email protected], and destroy all copies of this
> message and any attachments.
>

Reply via email to