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?
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: