You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jerry Jalenak <Je...@LABONE.com> on 2002/06/28 15:46:46 UTC

Error Handling

This is a little off, but since it applies to the separation of business
logic from the view, I thought I'd ask it here first.

When an exception is thrown in a BO or DAO level class, what is the
'correct' method of propogating the exception back up to the Action?  I can
catch these exceptions, handle them appropriately, and then throw new
exceptions.  What I've never figured out is how to 'catch' the new exception
at the next higher level.  What I'm trying to do is gracefully present a
user-friendly message and not a stack trace....


Thanks.

Jerry

This transmission (and any information attached to it) may be confidential and is intended solely for the use of the individual or entity to which it is addressed. If you are not the intended recipient or the person responsible for delivering the transmission to the intended recipient, be advised that you have received this transmission in error and that any use, dissemination, forwarding, printing, or copying of this information is strictly prohibited. If you have received this transmission in error, please immediately notify LabOne at (800)388-4675.



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Error Handling

Posted by Karim Saloojee <ka...@hotpop.com>.
What we have done is extend the Action class with our own BaseAction, all
our Actions now extend BaseAction. Thus we can throw DomainExceptions in our
actions, which are caught in the BaseAction and the message is displayed to
the user in a fairly nice way.

There is a chapter on TheServerSide (link below) on exception handling (Chap
10), I think it should tell you what you want to know.

http://www.theserverside.com/resources/strutsreview.jsp

~Karim


----- Original Message -----
From: "Jerry Jalenak" <Je...@LABONE.com>
To: <st...@jakarta.apache.org>
Sent: Friday, June 28, 2002 3:46 PM
Subject: Error Handling


> This is a little off, but since it applies to the separation of business
> logic from the view, I thought I'd ask it here first.
>
> When an exception is thrown in a BO or DAO level class, what is the
> 'correct' method of propogating the exception back up to the Action?  I
can
> catch these exceptions, handle them appropriately, and then throw new
> exceptions.  What I've never figured out is how to 'catch' the new
exception
> at the next higher level.  What I'm trying to do is gracefully present a
> user-friendly message and not a stack trace....
>
>
> Thanks.
>
> Jerry
>
> This transmission (and any information attached to it) may be confidential
and is intended solely for the use of the individual or entity to which it
is addressed. If you are not the intended recipient or the person
responsible for delivering the transmission to the intended recipient, be
advised that you have received this transmission in error and that any use,
dissemination, forwarding, printing, or copying of this information is
strictly prohibited. If you have received this transmission in error, please
immediately notify LabOne at (800)388-4675.
>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Error Handling

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
Hi Jerry,

I was looking at this today.

I hope to come up with something that is generally applicable in all my 
actions.

For example I need to catch an SQL Exception caused by a constraint in 
the database, and return the error message "sorry, but you can't do 
that...."

I figured I am only going to need a finite number of user-friendly 
messages, and I'll keep these in my resource bundle, and (this is the 
bit I'm not sure about) I'd probably create an Exception subclass for 
each one.

I'll just have a function in my catch block which I give the exception 
type or number or whatever, and it gives me back the error message, via 
a large switch case block.

What do you reckon to that?


Adam



Jerry Jalenak wrote:
> This is a little off, but since it applies to the separation of business
> logic from the view, I thought I'd ask it here first.
> 
> When an exception is thrown in a BO or DAO level class, what is the
> 'correct' method of propogating the exception back up to the Action?  I can
> catch these exceptions, handle them appropriately, and then throw new
> exceptions.  What I've never figured out is how to 'catch' the new exception
> at the next higher level.  What I'm trying to do is gracefully present a
> user-friendly message and not a stack trace....
> 
> 
> Thanks.
> 
> Jerry
> 
> This transmission (and any information attached to it) may be confidential and is intended solely for the use of the individual or entity to which it is addressed. If you are not the intended recipient or the person responsible for delivering the transmission to the intended recipient, be advised that you have received this transmission in error and that any use, dissemination, forwarding, printing, or copying of this information is strictly prohibited. If you have received this transmission in error, please immediately notify LabOne at (800)388-4675.
> 
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Error Handling

Posted by Robert Taylor <rt...@mulework.com>.
In general, you can propagate the exceptions from the business layer to the
presentation layer (Action class) where it is caught and based on the type
of exception, the appropriate error message can be looked up in your message
resources and saved in the request scope and you can then forward the user
to the appropriate error handler page.

Another alternative would be to take advantage of the new exception handling
in Struts1.1x. You can declaritively define exception handlers in your
struts-config.xml and then your Action class does not have to explicitely
contain the logic to deal with them.

HTH,

robert

> -----Original Message-----
> From: Jerry Jalenak [mailto:Jerry.Jalenak@LABONE.com]
> Sent: Friday, June 28, 2002 9:47 AM
> To: 'struts-user@jakarta.apache.org'
> Subject: Error Handling
>
>
> This is a little off, but since it applies to the separation of business
> logic from the view, I thought I'd ask it here first.
>
> When an exception is thrown in a BO or DAO level class, what is the
> 'correct' method of propogating the exception back up to the
> Action?  I can
> catch these exceptions, handle them appropriately, and then throw new
> exceptions.  What I've never figured out is how to 'catch' the
> new exception
> at the next higher level.  What I'm trying to do is gracefully present a
> user-friendly message and not a stack trace....
>
>
> Thanks.
>
> Jerry
>
> This transmission (and any information attached to it) may be
> confidential and is intended solely for the use of the individual
> or entity to which it is addressed. If you are not the intended
> recipient or the person responsible for delivering the
> transmission to the intended recipient, be advised that you have
> received this transmission in error and that any use,
> dissemination, forwarding, printing, or copying of this
> information is strictly prohibited. If you have received this
> transmission in error, please immediately notify LabOne at (800)388-4675.
>
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Error Handling

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 28 Jun 2002, Jerry Jalenak wrote:

> Date: Fri, 28 Jun 2002 08:46:46 -0500
> From: Jerry Jalenak <Je...@LABONE.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: "'struts-user@jakarta.apache.org'" <st...@jakarta.apache.org>
> Subject: Error Handling
>
> This is a little off, but since it applies to the separation of business
> logic from the view, I thought I'd ask it here first.
>
> When an exception is thrown in a BO or DAO level class, what is the
> 'correct' method of propogating the exception back up to the Action?  I can
> catch these exceptions, handle them appropriately, and then throw new
> exceptions.  What I've never figured out is how to 'catch' the new exception
> at the next higher level.  What I'm trying to do is gracefully present a
> user-friendly message and not a stack trace....
>

Well, the "catch the exception" part is pretty easy :-)

  try {
    businessObject.someBusinessMethod();
  } catch (Exception e) {
    ... do something with exception e ...
  }

The interesting challenge is what to do with it.  In Struts 1.0, you
basically *must* catch and swallow checked exceptions (because there is no
"throws" on the perform() method), and you *should* catch runtime errors
(like null pointer exceptions).  You would need to have code in your
Action that determined what page to go to next to display the error, and
then return an appropriate ActionForward.

In Struts 1.1, you have an additional option if you switch to using the
execute() method -- declarative exception handling.  Basically, you
configure (in struts-config.xml) what happens when an Action throws
exceptions of particular classes.  Like forwards, there is the notion of
global exception handler configurations, with local per-action overrides.

Essentially, this lets you delegate the "what do I show the user" question
out of your Action that interacts with the business logic, so you can
create cleaner and more focused Action classes.

>
> Thanks.
>
> Jerry
>

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>