You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Francisco Exposito Aguilera <fc...@hotmail.com> on 2007/02/04 18:01:00 UTC

Exceptions design

Hello all,

I have a doubt about how design the exceptions in my struts project. I have 
the action classes and the bean classes. The action classes execute methods 
from bean classes, which connect to the database.

Supposing I am trying to create a user in one of my web pages and the user I 
want to create exists. What is the best way to show the user this sql error?

1.Throw the exception in the bean class to the struts ModuleException and 
show a typical error page. (I hope this is not the best way because I don´t 
like it).

2.Before execute the createUser bean method, execute another method to check 
if the user already exists. If it exists, use ActionMessages in the action 
class to show the error in the same createUser.jsp file.

That means that if the user doesn´t exist, the action method will access, at 
least twice to the database, one for check if the user exists and one for 
create it.

3.Execute the createUser bean method using try and finally but without 
catch; and throw the SQL exception.

public OTDUser validUser (String dni, String passwd) throws SQLException
{
	try
	{
		Connection to the database and results
	}
	finally
	{
		Close resultSet, statement and connection
	}
}

The exception will be catched by the action class, where there is a 
catch(SQLException), which will receive the exception from the bean method

java.sql.SQLException: ORA-00001: unique restriction (PK_USER) violated

And with ActionMessages show it in the same createUser.jsp. But now, can I 
know if the SQLException is caused by:

a) Primary key is violated (java.sql.SQLException: ORA-00001: unique 
restriction (PK_USER) violated)

b) Name and both surnames are unique (java.sql.SQLException: ORA-00001: 
unique restriction (U_NAME_SURNAME1_SURNAME2_U) violated)

Depending on the exception, I want to show “primary key exists” or “name and 
surnames exists with another primary key”

4.Any suggestion?

Thanks a lot.

_________________________________________________________________
Horóscopo, tarot, numerología... Escucha lo que te dicen los astros. 
http://astrocentro.msn.es/


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Exceptions design

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

Piero Sartini wrote:
> I have a rule: If something is defined and I know it will happen often, it 
> should not be an exception. It is a path in the application.

There's always the (slim) possibility that the user will not be found
during the lookup, but will then be there by the time you try to
actually create the record. In that case, you WILL get a SQLException
because the user already exists, and you'll probably want to handle it
in the same way as if you had detected the collision in the first place.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFx0qb9CaO5/Lv0PARAhI8AKCj0xHiGpzz9PBeVOt/X8yvzGcTqgCgwg0f
mtxpAh9H9C0kHQLIX/I1OPg=
=YZVB
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Exceptions design

Posted by Piero Sartini <li...@pierosartini.de>.
Hello,

it sure is a matter of taste - but I think its not good to use exceptions for 
this kind of things. After all "User not found" is not an _exception_. Its a 
well defined case and it happens frequently. (Except you rely on that the 
user exists, eg. in step 2 of a registration process)

I have a rule: If something is defined and I know it will happen often, it 
should not be an exception. It is a path in the application.

Am Sonntag, 4. Februar 2007 18:01 schrieb Francisco Exposito Aguilera:
> Hello all,
>
> I have a doubt about how design the exceptions in my struts project. I have
> the action classes and the bean classes. The action classes execute methods
> from bean classes, which connect to the database.
>
> Supposing I am trying to create a user in one of my web pages and the user
> I want to create exists. What is the best way to show the user this sql
> error?
>
> 1.Throw the exception in the bean class to the struts ModuleException and
> show a typical error page. (I hope this is not the best way because I don´t
> like it).
>
> 2.Before execute the createUser bean method, execute another method to
> check if the user already exists. If it exists, use ActionMessages in the
> action class to show the error in the same createUser.jsp file.
>
> That means that if the user doesn´t exist, the action method will access,
> at least twice to the database, one for check if the user exists and one
> for create it.
>
> 3.Execute the createUser bean method using try and finally but without
> catch; and throw the SQL exception.
>
> public OTDUser validUser (String dni, String passwd) throws SQLException
> {
> 	try
> 	{
> 		Connection to the database and results
> 	}
> 	finally
> 	{
> 		Close resultSet, statement and connection
> 	}
> }
>
> The exception will be catched by the action class, where there is a
> catch(SQLException), which will receive the exception from the bean method
>
> java.sql.SQLException: ORA-00001: unique restriction (PK_USER) violated
>
> And with ActionMessages show it in the same createUser.jsp. But now, can I
> know if the SQLException is caused by:
>
> a) Primary key is violated (java.sql.SQLException: ORA-00001: unique
> restriction (PK_USER) violated)
>
> b) Name and both surnames are unique (java.sql.SQLException: ORA-00001:
> unique restriction (U_NAME_SURNAME1_SURNAME2_U) violated)
>
> Depending on the exception, I want to show “primary key exists” or “name
> and surnames exists with another primary key”
>
> 4.Any suggestion?
>
> Thanks a lot.
>
> _________________________________________________________________
> Horóscopo, tarot, numerología... Escucha lo que te dicen los astros.
> http://astrocentro.msn.es/
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Exceptions design

Posted by Francisco Exposito Aguilera <fc...@hotmail.com>.


>From: Dave Newton <ne...@yahoo.com>
>Reply-To: "Struts Users Mailing List" <us...@struts.apache.org>
>To: Struts Users Mailing List <us...@struts.apache.org>
>Subject: Re: Exceptions design
>Date: Sun, 4 Feb 2007 09:41:23 -0800 (PST)
>
>--- Francisco Exposito Aguilera wrote:
> > What is the best way to show the user this sql
>error?
>
>I'm skeptical that you'd want to show the *user* an
>SQL error; my experience has been that it's best to
>wrap up such exceptions in an application-specific
>exception and report a general system error (or even a
>database error) and leave the specifics to a log
>message, email or IM to the admin, etc.
>
> > 2.Before execute the createUser bean method, execute
> > another method to check if the user already exists.
> > If it exists, use ActionMessages in the action
> > class to show the error in the same createUser.jsp
> > file.
> >
> > That means that if the user doesn´t exist, the
> > action method will access, at least twice to the
> > database, one for check if the user exists and one
> > for create it.
>
>I would think you'd have to do that anyway.
>
> > java.sql.SQLException: ORA-00001: unique restriction
> > (PK_USER) violated
> >
> > a) Primary key is violated (java.sql.SQLException:
> > ORA-00001: unique
> > restriction (PK_USER) violated)
> >
> > b) Name and both surnames are unique
> > (java.sql.SQLException: ORA-00001:
> > unique restriction (U_NAME_SURNAME1_SURNAME2_U)
> > violated)
>
>I would most certainly *not* expose *any* system
>internals on a user-accessible web page.
>
> > Depending on the exception, I want to show “primary
> > key exists” or “name and surnames exists with
>another
> > primary key”
>
>Useful for developer, useless for user.
>
>All the *user* needs to know that another user with
>the same username exists and they need to choose
>another username or if they forgot their password they
>can click here, etc.) Keep user messages simple and
>jargon-free. It's easy enough to capture complete
>failure info for the developer somewhere else.
>
>(You can't restrict on just their real name because
>people have the same names sometimes.)
>
>d.
>
>
>
>
>____________________________________________________________________________________
>Don't get soaked.  Take a quick peak at the forecast
>with the Yahoo! Search weather shortcut.
>http://tools.search.yahoo.com/shortcuts/#loc_weather
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>

_________________________________________________________________
Grandes éxitos, superhéroes, imitaciones, cine y TV... 
http://es.msn.kiwee.com/ Lo mejor para tu móvil.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Exceptions design

Posted by Dave Newton <ne...@yahoo.com>.
--- Francisco Exposito Aguilera wrote:
> I am doing just now a part for an administrator user

> and I want to show the error most clear as possible.


Oh, if it's for an admin that would understand and be
able to do something, then sure, be as detailed as you
want :)

> a) I must do an extra request to the database in
> order to know if the user exists to show the user
the
> ActionMessages.

Well... you can rely on getting a database constraint
violation if you want; I don't know as it *really*
matters. I guess I've just always done a lookup first,
as (in my mind, anyway) it's not really "exceptional"
that someone might choose the same username as
somebody else.

I tend to view database exceptions as something that
shouldn't happen unless something is really *wrong*
wrong.

There may not be a better reason than preference on
this issue :)

Seriously, I'm not sure there's any one right way to
do this; the declarative exception handling lets the
developer decide what exceptions to handle in the
framework, or they can be caught earlier and
ActionMessages can be used.

d.



 
____________________________________________________________________________________
It's here! Your new message!  
Get new email alerts with the free Yahoo! Toolbar.
http://tools.search.yahoo.com/toolbar/features/mail/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Exceptions design

Posted by Francisco Exposito Aguilera <fc...@hotmail.com>.
I agree  with you about don´t show SQL errors to a “normal” user… I forgot 
to say that I am doing just now a part for an administrator user and I want 
to show the error most clear as possible. Maybe I should use a log message.

Once it is defined that (tell me if I haven´t understood you):

a) I must do an extra request to the database in order to know if the user 
exists to show the user the ActionMessages.
b) I must create a general page error for another errors (using 
ModuleException, for example?)

Are the next steps correct?

1) Use the action methods for “local” errors. For local errors I mean the 
errors which will be shown to the user at the same page where it is the 
form.
2) Use the bean methods for “general” errors. For general error I mean an 
error which will be shown at an error page. (using ModuleException).

Thanks



>From: Dave Newton <ne...@yahoo.com>
>Reply-To: "Struts Users Mailing List" <us...@struts.apache.org>
>To: Struts Users Mailing List <us...@struts.apache.org>
>Subject: Re: Exceptions design
>Date: Sun, 4 Feb 2007 09:41:23 -0800 (PST)
>
>--- Francisco Exposito Aguilera wrote:
> > What is the best way to show the user this sql
>error?
>
>I'm skeptical that you'd want to show the *user* an
>SQL error; my experience has been that it's best to
>wrap up such exceptions in an application-specific
>exception and report a general system error (or even a
>database error) and leave the specifics to a log
>message, email or IM to the admin, etc.
>
> > 2.Before execute the createUser bean method, execute
> > another method to check if the user already exists.
> > If it exists, use ActionMessages in the action
> > class to show the error in the same createUser.jsp
> > file.
> >
> > That means that if the user doesn´t exist, the
> > action method will access, at least twice to the
> > database, one for check if the user exists and one
> > for create it.
>
>I would think you'd have to do that anyway.
>
> > java.sql.SQLException: ORA-00001: unique restriction
> > (PK_USER) violated
> >
> > a) Primary key is violated (java.sql.SQLException:
> > ORA-00001: unique
> > restriction (PK_USER) violated)
> >
> > b) Name and both surnames are unique
> > (java.sql.SQLException: ORA-00001:
> > unique restriction (U_NAME_SURNAME1_SURNAME2_U)
> > violated)
>
>I would most certainly *not* expose *any* system
>internals on a user-accessible web page.
>
> > Depending on the exception, I want to show “primary
> > key exists” or “name and surnames exists with
>another
> > primary key”
>
>Useful for developer, useless for user.
>
>All the *user* needs to know that another user with
>the same username exists and they need to choose
>another username or if they forgot their password they
>can click here, etc.) Keep user messages simple and
>jargon-free. It's easy enough to capture complete
>failure info for the developer somewhere else.
>
>(You can't restrict on just their real name because
>people have the same names sometimes.)
>
>d.
>
>
>
>
>____________________________________________________________________________________
>Don't get soaked.  Take a quick peak at the forecast
>with the Yahoo! Search weather shortcut.
>http://tools.search.yahoo.com/shortcuts/#loc_weather
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>

_________________________________________________________________
Dale rienda suelta a tu tiempo libre. Mil ideas para exprimir tu ocio con 
MSN Entretenimiento. http://entretenimiento.msn.es/


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Exceptions design

Posted by Dave Newton <ne...@yahoo.com>.
--- Francisco Exposito Aguilera wrote:
> What is the best way to show the user this sql
error?

I'm skeptical that you'd want to show the *user* an
SQL error; my experience has been that it's best to
wrap up such exceptions in an application-specific
exception and report a general system error (or even a
database error) and leave the specifics to a log
message, email or IM to the admin, etc.

> 2.Before execute the createUser bean method, execute
> another method to check if the user already exists. 
> If it exists, use ActionMessages in the action 
> class to show the error in the same createUser.jsp
> file.
> 
> That means that if the user doesn´t exist, the
> action method will access, at least twice to the 
> database, one for check if the user exists and one 
> for create it.

I would think you'd have to do that anyway.

> java.sql.SQLException: ORA-00001: unique restriction
> (PK_USER) violated
> 
> a) Primary key is violated (java.sql.SQLException:
> ORA-00001: unique 
> restriction (PK_USER) violated)
> 
> b) Name and both surnames are unique
> (java.sql.SQLException: ORA-00001: 
> unique restriction (U_NAME_SURNAME1_SURNAME2_U)
> violated)

I would most certainly *not* expose *any* system
internals on a user-accessible web page.

> Depending on the exception, I want to show “primary
> key exists” or “name and surnames exists with
another
> primary key”

Useful for developer, useless for user. 

All the *user* needs to know that another user with
the same username exists and they need to choose
another username or if they forgot their password they
can click here, etc.) Keep user messages simple and
jargon-free. It's easy enough to capture complete
failure info for the developer somewhere else.

(You can't restrict on just their real name because
people have the same names sometimes.)

d.



 
____________________________________________________________________________________
Don't get soaked.  Take a quick peak at the forecast
with the Yahoo! Search weather shortcut.
http://tools.search.yahoo.com/shortcuts/#loc_weather

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org