• Post Reply Bookmark Topic Watch Topic
  • New Topic

Distinguish a NoResultException, from an InvocationTargetException  RSS feed

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
I have an ejb method which if on DB doesn't find the entry requested should throws a javax.persistence.NoResultException.
On the other hand I have standalone client application that acts as tester for the ejb method.

On the logic application of this client I'd need to catch the NoResultException, but instead looks like I get a java.lang.reflect.InvocationTargetException. Although this exception could be acceptable, is too generic for my purposes.
If from that I call the getMessage() I actually have javax.ejb.EJBException: javax.persistence.NoResultException: No entity found for query.

How can distinguish InvocationTargetException thrown by a NoResultException?

Thanks in advance

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getCause()?
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


something more elegant?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really. Some frameworks (including dynamically instantiating classes using Class.newInstance or Constructor.newInstance) simply wrap the exceptions thrown into a single exception, in this case InvocationTargetException. If they didn't these methods would have to declare Exception in their throws clause, or even worse, Throwable. Unless you rebuild the entire framework you're stuck with this behaviour.

getCause() is the only way to retrieve the original exception, then use an if-statement:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!