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.
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.??? thx
Hi, 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. Cheers, Gaya3
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: