| Author |
The best way to catch Oracle exceptions
|
Danilo Dadonas
Ranch Hand
Joined: Aug 24, 2007
Posts: 59
|
|
Hi all. I created some exceptions to validate inserts in Oracle because I want to show a friendly message for the user case some error (like PK, FK) occurs. I want to know if it's the best way. Look at the code: In method insertCustomer I get the SQlException.errorCode() as the Oracle error code and trhow the corresponding exception like PKException, FKException... Is it a good idea? Is there another best way? Thanks
|
Dadonas<br /> <br />Don't gain the world and lose your soul.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26193
|
|
Danilo, Friendly to whom? Do your users know what a primary key is? Would they do something different if they knew this?
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Danilo Dadonas
Ranch Hand
Joined: Aug 24, 2007
Posts: 59
|
|
Hi Jeanne, when I said "System.out.println("invalid primary key");", I just mentioned an example. Certainly this message will be like this: "System.out.println("This item can't be inserted because it's already registered");". I want to know if these validations that I mentioned are the best way. There is some package of Oracle or else that do it? Thanks.
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
Jeanne's question is still a good one. Would the user do anything different if they knew this? If they are the sort of user who understands how to resolve a FK violation for example, and has access to do this, they can probably survive with the SQLExceptions as is, since they will be no different from the messages Oracle returns normally (SQLExceptions just wrap the output from the database). If you have to do this, I'd avoid explicitly including Oracle specific classes - instead perhaps regex the SQLException message and watch for known Oracle error codes? This way your code wont break if you change databases.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Danilo Dadonas
Ranch Hand
Joined: Aug 24, 2007
Posts: 59
|
|
Paul, Jeanne, you're right. The end user shouldn't know what is going on during the insert. But during the transaction, if it throw a SQLException, I need to know what caused this exception to do something (it can be show a friendly message for example). SQLException is generic, it can be caused by a PK, FK, UK... The unique way that I found was getting SQLException.getErrorCode(). For example, the code 1 (for the Oracle) is Primary Key Violation... In DAO classes I throw an especified exception depending the getErrorCode(). Like this: The interface will call this method and implement the PKException: Did you understand me? Now I want to know: Is it the best way? There are something newer than this solution? Tahnks.
|
 |
 |
|
|
subject: The best way to catch Oracle exceptions
|
|
|