• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The best way to catch Oracle exceptions

 
Ranch Hand
Posts: 59
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Danilo,
Friendly to whom? Do your users know what a primary key is? Would they do something different if they knew this?
 
Danilo Dadonas
Ranch Hand
Posts: 59
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Danilo Dadonas
Ranch Hand
Posts: 59
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic