ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Lamey" <cla...@localmatters.com>
Subject RE: How to custom database exception?
Date Sat, 29 Dec 2007 14:46:45 GMT
Hello, my name is Chris and I've committed this most egregious hack.

Many years ago when I was young and easily led down the dark side, a requirement came down
from on high that required a custom user message for a specific Oracle error message.  The
project was already through the development and test phase.  Someone with an impressive title
had seen that exception in production and it had to be taken care of.  The requirement came
at me late at night and had to be implemented immediately - there was no time to think!

So in the catch block of SQLException, I tested for the specific Oracle error ID in the exception
mesage.  If it was there, I threw one of our custom exceptions that produced the desired effect
in the application.

At the time, I commented that code as something that was a hack and had to be changed.  I
promised myself I would go back and fix it.  But alas, this did not come to pass and that
code is still running.

It was to easy to get done at the time...it haunts me to this day.

If they switch DBs, even versions of Oracle, that code is easily broken.  Trying to map DB
specific error codes to an Exception hierarchy is madness.  Instead, it is better to test
for those conditions and constraints up front and not to let it be an error condition.

It's also good to read Josh Bloch's Effective Java for the section on Exception handling.
 He makes a pretty strong case for not using custom Exceptions as what amounts to return codes
for commonly expected cases.

Cheers,
Chris

-----Original Message-----
From: larry.meadors@gmail.com on behalf of Larry Meadors
Sent: Sat 12/29/2007 6:35 AM
To: user-java@ibatis.apache.org
Subject: Re: How to custom database exception?
 
That's a SQL/JDBC problem, really - a unique constraint doesn't give
you much in terms of customizing the message. I guess if you wanted
to, you could look at the message and try to map it to a nicer
message...but iBATIS isn't going to do that for you. It would be
infeasible to map any database error message to custom exceptions.

Larry


On Dec 28, 2007 10:59 PM, yuan gogo <gogoyuan@gmail.com> wrote:
> Hi, all.
>     I'm new to iBatis, and wonder how to get custom data exception in the
> web application.
>
>    Say, I have a database table Users (id, username, password), the username
> is UNIQUE.
>    While insert new user data with a duplicated username , a data exception
> will be thrown from jdbc indicate constraint conflict on some datafileds.
> But I personally think throw a custom exception like
> UsernameExistedException will be a better way for other layer to log or
> notify user.
>
>    How can I do it?
>
> Ps : I use Tapestry 5 + spring 2.5M + iBatis 2 + postgresql 8.3 beta +
> tomcat 6.0.14 for evaluation.
>
> Thanks!
>
> gogoyuan [_at_] gmail.com
>


Mime
View raw message