How to extract from the exception the reason for the failure

I

Itamar Lev

I have a method that executes an sql update and fails.
The description of the failure is:

com.ibm.websphere.ce.cm.DuplicateKeyException: One or more values in
the INSERT statement, UPDATE statement, or foreign key update caused
by a DELETE statement are not valid because the primary key, unique
constraint or unique index identified by "1" constrains table "XXX"
from having duplicate rows for those columns.

I didn't find a way of extracting the type of the exception (if
there's one).
I want to be able to assess the exception and to return a specific
error message with
the specific error explained.

thanks
Itamar
 
V

VisionSet

Itamar Lev said:
I have a method that executes an sql update and fails.
The description of the failure is:

com.ibm.websphere.ce.cm.DuplicateKeyException: One or more values in
the INSERT statement, UPDATE statement, or foreign key update caused
by a DELETE statement are not valid because the primary key, unique
constraint or unique index identified by "1" constrains table "XXX"
from having duplicate rows for those columns.

I didn't find a way of extracting the type of the exception (if
there's one).
I want to be able to assess the exception and to return a specific
error message with
the specific error explained.

Throwable.getMessage()

Throwable.toString() gives a bit more.
 
S

Sudsy

Itamar said:
I have a method that executes an sql update and fails.
The description of the failure is:

com.ibm.websphere.ce.cm.DuplicateKeyException: One or more values in
the INSERT statement, UPDATE statement, or foreign key update caused
by a DELETE statement are not valid because the primary key, unique
constraint or unique index identified by "1" constrains table "XXX"
from having duplicate rows for those columns.

I didn't find a way of extracting the type of the exception (if
there's one).

It's right there in back and white:

com.ibm.websphere.ce.cm.DuplicateKeyException

It should properly extend java.sql.SQLException so you should be able
to invoke getErrorCode for the vendor-specific error code (int) or
getSQLState to get a string representation.
The problem is that you're trying to set a column to an invalid
value. It could be:
- you're trying to update the primary key (or part of it) and the
unique key already exists
- you're trying to insert a value in a column which is a foreign
key into another table which doesn't have a key corresponding
the the specified value
- you're breaking some other uniqueness constraint
Take a look at the constraints for the table. You might find it
necessary to perform lookups on this table or others before
attempting the update if you want to handle it programmatically.
Invoking getMessage and then following the chain via
getNextException might not provide helpful feedback for your
users.
 
J

Job Numbers

Itamar Lev said:
I have a method that executes an sql update and fails.
The description of the failure is:

com.ibm.websphere.ce.cm.DuplicateKeyException: One or more values in
the INSERT statement, UPDATE statement, or foreign key update caused
by a DELETE statement are not valid because the primary key, unique
constraint or unique index identified by "1" constrains table "XXX"
from having duplicate rows for those columns.

I didn't find a way of extracting the type of the exception (if
there's one).
I want to be able to assess the exception and to return a specific
error message with
the specific error explained.

thanks
Itamar

Spring's jdbc library is a thin wrapper around jdbc that makes it easier to
use and converts all the checked exception garbage to runtime exceptions.
It also creates a very conprehensive set of specific exceptions to make it
easier to find out what happened and converts vender specific codes to the
proper exception class. Essentially, it makes jdbc code cleaner and gives
you more debugging power.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top