• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why i can't catch ConstraintViolationException exception ?

 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day,

In order to avoid record insertion with same unique value, i try following but it seem doesn't catch the ConstraintViolationException at all but i got the error show below:




Error that i experiencing:


Appreciated your guidance!
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that user.InsertRecord() throws a ConstraintViolationException? It seems that SQLException would be more likely. That log statement you're getting is definitely based on a SQLException.

(Note: Don't capitalize method names. user.insertRecord(paramUser) is in line with Java coding standards.)
 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg, Thanks for reply!

in the debugger, it show me persistenceException and the cause is ConstraintViolationException, if i using SQLEXception, it doesn't allow to put try catch and prompt below message.



p/s : using hibernate and JPA
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's confusing then. The JavaDoc for org.hibernate.engine.jdbc.spi.SqlExceptionHelper says:

Helper for handling SQLExceptions in various manners.


If it doesn't get thrown out of InsertRecord(), then the SQLException is being handled inside InsertRecord() or something that it calls. That handler might rethrow a ConstraintViolationException. I don't have Hibernate set up now, so I can't test that. When you say, you see it in the debugger, do you mean an actual debugger stopped at a break point, or are you seeing it in a log file? If it's a break point, then at which line is it?

I can only think of a couple of ways the catch clause at lines 7 - 9 could fail to catch the exception:

1. The exception is thrown from somewhere else other than the try block.
2. There are more than one class named ContraintViolationException, but in different packages, and your imports are set so that you are not catching the type that is being thrown.

Those are pretty much the only possibilities.
 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could be right Greg, i observed the exception throw before the persistence exception, but how can we handle it ?

 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand what's going on. When you said, "in the debugger, it show me persistenceException and the cause is ConstraintViolationException", you mean you saw a stack track (a series of class names and line numbers), and then a "Caused By", and another stack trace. That means the type of the exception is really PersistenceException. It wraps a ConstraintViolationException, but that's just to provide you with more information about where and why the exception occurred. It doesn't affect the program flow in any way. So, since the code in the try block throws a PersistenceException, the catch block must catch a PersistenceException (or one of its superclasses up to Throwable). The latest code posting you made should work.
 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg, what i plan is really throw the exception if it is ConstraintViolationException exception bacause some fields in database have been set as unique(ie:username) and to ensure no duplicate record, persistenceException seem very general in this case, how could we handle and throw if the PersistenceException exception wrapped with ConstraintViolationException ?
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If PersisitenceException is what's thrown by (I assume) code you can't control, then you have no choice but to catch and handle that. In the handler (i.e., catch block), if you absolutely have to, you can extract the wrapped ConstraintViolationException using the getCause() method, and from there you can proceed exactly as if that was the exception you caught. I don't exactly recommend that, but it is possible.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic