I guess it depends on the technology you use to access your database (JDBC, O/R mapping etc.) what kind of information you will get in case of a constraint violation. JPA, Hibernate and other O/R mappers usually tell you in a more convenient way what kind of constraint violation (i.e. duplicate key) occurred whereas you have to deal with more low-level details if you use a more low-level API like JDBC.
For example you can get some details out of the information encapsulated inside an ordinary SQLException. According to the documentation you can expect the following content although I must admit, I'm unsure how to figure out exactly what went wrong in case of a constraint violation:
Each SQLException provides several kinds of information:
a string describing the error. This is used as the Java Exception message, available via the method getMesasge.
a "SQLstate" string, which follows either the XOPEN SQLstate conventions or the SQL:2003 conventions. The values of the SQLState string are described in the appropriate spec. The DatabaseMetaData method getSQLStateType can be used to discover whether the driver returns the XOPEN type or the SQL:2003 type.
an integer error code that is specific to each vendor. Normally this will be the actual error code returned by the underlying database.
a chain to a next Exception. This can be used to provide additional error information.
the causal relationship, if any for this SQLException.
A cleaner way would probably be to check for existing database entries before you insert the new data records or check for return values the corresponding API methods offer you.
But as I said it depends on the API/technology you use. So to get more help, it would be helpful for us if you could tell us a little bit more details