0
javax.ejb.EJBException: java.lang.reflect.InvocationTargetException; nested exception is:
        com.emirates.jfoundation.exception.ServiceException: java.lang.reflect.InvocationTargetException; nested exception is: com.emirates.jfoundation.exception.ServiceException: java.lang.reflect.InvocationTargetException
com.emirates.jfoundation.exception.ServiceException: java.lang.reflect.InvocationTargetException
        at weblogic.utils.StackTraceDisabled.unknownMethod()
Caused by: java.lang.reflect.InvocationTargetException
        ... 1 more
Caused by: EvaluationException [oldEvaluationRatesDTO=null, **newEvaluationRatesDTO** =ShipmentEvaluationRatesDTO [id=null,

I need to read the values in newEvaluationRatesDTO. How do I catch EvaluationException from the above stack trace.

themaster
  • 453
  • 11
  • 32

2 Answers2

1

If you want to find a specific exception in a nested chain, you can do so by looping on Throwable.getCause(). So a method like this will extract the required exception (if it exists):

@SuppressWarnings("unchecked")
public static <T extends Throwable> T getNestedExcetpion(Throwable caught,
         Class<T> exceptionType) {
    while (caught != null) {
        if (exceptionType.isInstance(caught))
            return (T)caught;
        caught = caught.getCause();
    }
    return null;
}

You can then use this in a catch clause:

    try {
       // do something that may throw
    } catch (Exception e) {
        EvaluationException myerr = getNestedException(e, EvaluationException.class);
        if (myerr != null) {
            // extract info from myerr
        }
    }

If the top level exception is an EJBException, it seems that some implementations of J2EE do not use getCause() to retrieve the nested exception from the EJBException.

To work around that issue, you might want to catch it like so:

    try {
       // do something that may throw an EJBException
    } catch (EJBException e) {
        EvaluationException myerr = getNestedException(e.getCausedByException(),
             EvaluationException.class);
        if (myerr != null) {
            // extract info from myerr
        }
    }
Guss
  • 30,470
  • 17
  • 104
  • 128
  • myerr is coming as null even though we have EvaluationException in the stack trace. – themaster Dec 20 '22 at 14:10
  • If an exception of type `EvaluationException` is available from a `getCause()` call on the stack represented by the chain of `getCause()` calls, then the code above will find it. I suggest debugging through the code and making sure that the data you expect to be there is indeed there. – Guss Dec 20 '22 at 14:13
  • I cannot debug due to some reasons. I can only put print statements. Anything I can print which would help to analyze this.? – themaster Dec 20 '22 at 14:20
  • You may want to make sure that the `getCause()` return value from the first exception is not null (or maybe just add a print statement for each iteration in `getNestedException()`). – Guss Dec 20 '22 at 14:24
  • EJBException myerr = getNestedException(e, EJBException.class); -- Only this is getting caught. ServiceException or EvaluationException is not getting caught. – themaster Dec 20 '22 at 14:25
  • Looking a bit into `EJBException`, I stumbled into this article: https://www.fokkog.com/2013/05/ejbexception-getcausedbyexception.html - it implies that with some `EJBException` implementations, `getCause()` doesn't actually work and you need to use another method to access the nested exception. Maybe you should try to call `getNestedExcetion()` with the result of `getCausedByException()` of the original `EJBException` to start the drill down. – Guss Dec 20 '22 at 14:25
  • I'll update my answer. – Guss Dec 20 '22 at 14:27
0

try something like this:


catch (EJBException e) {
   if (e.getCause() != null && e.getCause() instanceof EvaluationException) {
       //do something
   } else {
       // do something
   }
Yurii
  • 552
  • 3
  • 14