You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Lyallex <ly...@gmail.com> on 2008/07/09 18:00:53 UTC
Handling Exceptions in Action constructors
Hi
I have a question about handling Exceptions in Action constructors
I have the following constructor in an Action
public SomeAction() throws BusinessComponentException {
someComponent = new SomeBusinessComponent();
}
Now when an instance of this Action is instantiated it may be the case
that the SomeBusinessComponent
constructor may throw a BusinessComponentException, the question is
how to handle this.
As an aside It's also the case that the methods of this action may
also throw such an Exception
but I can manage that fine.
The exception trace reveals the following
Unable to instantiate Action, ...
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:294)
I tried to handle this by putting the following in struts.xml
<global-results>
<result name="error">/friendlyError.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
But no joy. I think this is because of the way the Exception
interceptor does it's thing.
Here's what my book (Struts2 in Action) says
...When the exception interceptor executes during its postprocessing
phase ... Hmm, that would explain it, there is no post processing
phase because execution doesn't get that far. (I think).
Does anyone have a pattern for handling exceptions in Action constructors ?
many TsIA
lyallex
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Handling Exceptions in Action constructors
Posted by Jim Kiley <jh...@summa-tech.com>.
The thing to remember here is that unlike the constructor, prepare() won't
run on its own outside of a Struts context. So if you are writing unit
tests of your various execute() type methods, be aware that you will need to
run prepare() manually within the unit tests (or as part of setUp() or
whatever).
jk
On Thu, Jul 10, 2008 at 3:59 AM, Lyallex <ly...@gmail.com> wrote:
> Sheesh, that works like a dream, thanks ...
> Can't think how I missed that bit in the book.
>
> Preparable, marvelous.
>
> lyallex
>
>
> On Wed, Jul 9, 2008 at 5:04 PM, Jim Kiley <jh...@summa-tech.com> wrote:
> > I think in this situation I would have SomeAction implement Preparable,
> and
> > put someComponent's initialization into the prepare() method, if I could
> get
> > away with it. I realize that might not be possible in your situation,
> but
> > not knowing more details it's my first suggestion.
> >
> > jk
> >
> > On Wed, Jul 9, 2008 at 12:00 PM, Lyallex <ly...@gmail.com> wrote:
> >
> >> Hi
> >>
> >> I have a question about handling Exceptions in Action constructors
> >>
> >> I have the following constructor in an Action
> >>
> >> public SomeAction() throws BusinessComponentException {
> >> someComponent = new SomeBusinessComponent();
> >> }
> >>
> >> Now when an instance of this Action is instantiated it may be the case
> >> that the SomeBusinessComponent
> >> constructor may throw a BusinessComponentException, the question is
> >> how to handle this.
> >>
> >> As an aside It's also the case that the methods of this action may
> >> also throw such an Exception
> >> but I can manage that fine.
> >>
> >> The exception trace reveals the following
> >>
> >> Unable to instantiate Action, ...
> >>
> >>
> com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:294)
> >>
> >> I tried to handle this by putting the following in struts.xml
> >>
> >> <global-results>
> >> <result name="error">/friendlyError.jsp</result>
> >> </global-results>
> >>
> >> <global-exception-mappings>
> >> <exception-mapping exception="java.lang.Exception" result="error"/>
> >> </global-exception-mappings>
> >>
> >> But no joy. I think this is because of the way the Exception
> >> interceptor does it's thing.
> >>
> >> Here's what my book (Struts2 in Action) says
> >>
> >> ...When the exception interceptor executes during its postprocessing
> >> phase ... Hmm, that would explain it, there is no post processing
> >> phase because execution doesn't get that far. (I think).
> >>
> >> Does anyone have a pattern for handling exceptions in Action
> constructors ?
> >>
> >> many TsIA
> >>
> >> lyallex
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >
> >
> > --
> > Jim Kiley
> > Technical Consultant | Summa
> > [p] 412.258.3346 [m] 412.445.1729
> > http://www.summa-tech.com
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
--
Jim Kiley
Technical Consultant | Summa
[p] 412.258.3346 [m] 412.445.1729
http://www.summa-tech.com
Re: Handling Exceptions in Action constructors
Posted by Lyallex <ly...@gmail.com>.
Sheesh, that works like a dream, thanks ...
Can't think how I missed that bit in the book.
Preparable, marvelous.
lyallex
On Wed, Jul 9, 2008 at 5:04 PM, Jim Kiley <jh...@summa-tech.com> wrote:
> I think in this situation I would have SomeAction implement Preparable, and
> put someComponent's initialization into the prepare() method, if I could get
> away with it. I realize that might not be possible in your situation, but
> not knowing more details it's my first suggestion.
>
> jk
>
> On Wed, Jul 9, 2008 at 12:00 PM, Lyallex <ly...@gmail.com> wrote:
>
>> Hi
>>
>> I have a question about handling Exceptions in Action constructors
>>
>> I have the following constructor in an Action
>>
>> public SomeAction() throws BusinessComponentException {
>> someComponent = new SomeBusinessComponent();
>> }
>>
>> Now when an instance of this Action is instantiated it may be the case
>> that the SomeBusinessComponent
>> constructor may throw a BusinessComponentException, the question is
>> how to handle this.
>>
>> As an aside It's also the case that the methods of this action may
>> also throw such an Exception
>> but I can manage that fine.
>>
>> The exception trace reveals the following
>>
>> Unable to instantiate Action, ...
>>
>> com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:294)
>>
>> I tried to handle this by putting the following in struts.xml
>>
>> <global-results>
>> <result name="error">/friendlyError.jsp</result>
>> </global-results>
>>
>> <global-exception-mappings>
>> <exception-mapping exception="java.lang.Exception" result="error"/>
>> </global-exception-mappings>
>>
>> But no joy. I think this is because of the way the Exception
>> interceptor does it's thing.
>>
>> Here's what my book (Struts2 in Action) says
>>
>> ...When the exception interceptor executes during its postprocessing
>> phase ... Hmm, that would explain it, there is no post processing
>> phase because execution doesn't get that far. (I think).
>>
>> Does anyone have a pattern for handling exceptions in Action constructors ?
>>
>> many TsIA
>>
>> lyallex
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
>
> --
> Jim Kiley
> Technical Consultant | Summa
> [p] 412.258.3346 [m] 412.445.1729
> http://www.summa-tech.com
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Handling Exceptions in Action constructors
Posted by Jim Kiley <jh...@summa-tech.com>.
I think in this situation I would have SomeAction implement Preparable, and
put someComponent's initialization into the prepare() method, if I could get
away with it. I realize that might not be possible in your situation, but
not knowing more details it's my first suggestion.
jk
On Wed, Jul 9, 2008 at 12:00 PM, Lyallex <ly...@gmail.com> wrote:
> Hi
>
> I have a question about handling Exceptions in Action constructors
>
> I have the following constructor in an Action
>
> public SomeAction() throws BusinessComponentException {
> someComponent = new SomeBusinessComponent();
> }
>
> Now when an instance of this Action is instantiated it may be the case
> that the SomeBusinessComponent
> constructor may throw a BusinessComponentException, the question is
> how to handle this.
>
> As an aside It's also the case that the methods of this action may
> also throw such an Exception
> but I can manage that fine.
>
> The exception trace reveals the following
>
> Unable to instantiate Action, ...
>
> com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:294)
>
> I tried to handle this by putting the following in struts.xml
>
> <global-results>
> <result name="error">/friendlyError.jsp</result>
> </global-results>
>
> <global-exception-mappings>
> <exception-mapping exception="java.lang.Exception" result="error"/>
> </global-exception-mappings>
>
> But no joy. I think this is because of the way the Exception
> interceptor does it's thing.
>
> Here's what my book (Struts2 in Action) says
>
> ...When the exception interceptor executes during its postprocessing
> phase ... Hmm, that would explain it, there is no post processing
> phase because execution doesn't get that far. (I think).
>
> Does anyone have a pattern for handling exceptions in Action constructors ?
>
> many TsIA
>
> lyallex
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
--
Jim Kiley
Technical Consultant | Summa
[p] 412.258.3346 [m] 412.445.1729
http://www.summa-tech.com