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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to