This week's book giveaway is in the OCAJP forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Exception management Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Exception management" Watch "Exception management" New topic

Exception management

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi all
I have a database with unique constraint. How can I differentiate the "unique constraint exception" (and manage this one in the application) and an another exception that i don't want to manage (just catch and no information to final user).
Do I have to check the constraint with a request before and throw my own exception or is it possible to recognize the exception.???
Gayathri Prasad
Ranch Hand

Joined: Jun 25, 2003
Posts: 116
When ever there is unique contraint violation the Database throws some number exception. We need to identify the exceptions depending on the number the DBengine returns. I think we dont have any exception classes for this and hence we need to code in our custom exception class.
Tim Morrow

Joined: Sep 17, 2003
Posts: 11
As mentioned previously there is a vendor-specific error code available from SQLException. You can check that value and handle it appropriately. You'd need to look up the particular error for your database and determine its error code. Take a look at the Javadoc for java.sql.SQLException.
Also, the original error may be nested a few exceptions deep, so you may also have to traverse the nested exceptions inside SQLException.
For example, assuming your "Unique constraint violated" error code is error code "1" for your database (believe this is correct for Oracle) you might write:
I agree. Here's the link:
subject: Exception management