4

I am sure we have all run into a scenario where a 3rd part API complains with an incomplete stack trace. In essence the error handling in the 3rd party API does something like this,

catch (WhateverException e) {
   throw new ConfusingException("Something terrible happened, BUT I won't tell you what!");
}

Now here when the ConfusingException is thrown it doesn't pass WhateverException as the cause in the second param, and hence when the someone using this API gets the exception they wouldn't get the whole trace. Sadly, it is still common to see people write such code and it gets worse when you have no control over the code.

What is the strategy you would follow when faced with the situation - how do you find the exception message and line on which WhateverException was raised?

8

Your best chance would be to run the application with a debugger and set a breakpoint on that line (or the constructor of ConfusingException).

It is possible to set a break point even in 3rd part applications when source may be unavailable. As in this case in the method or ConfusingException as suggested here.

  • Thanks Michael, voted up and added a link as to how to do it when source isn't available as the question indicated. – Yazad May 6 '15 at 22:32
  • Not every bad written code has a fix. I think there is not much that we can do. Check logs if they are at least logging the message using correlation Id – Breaking Benjamin Mar 19 at 12:58
-1

You can create an util function like this one :

public static Throwable getRootException(Throwable exception){
 Throwable rootException=exception;
 while(rootException.getCause()!=null){
  rootException = rootException.getCause();
 }
 return rootException;
}

Source : How to get the root exception of any exception

  • 2
    I think the point of the question was what to do when the cause was not included by the 3rd party exception. – doubleYou Feb 12 '18 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.