Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Distinguish a NoResultException, from an InvocationTargetException

 
Ranch Hand
Posts: 218
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getCause()?
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


something more elegant?
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic