File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
It's not a secret anymore!