Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which line in a long Java 'try' block is throwing an exception?

Is there a way to find out which line in a try block is throwing an exception?

I'm working on Java in Eclipse which looks like

try {

  //Lots of code. Seriously. Lots.

} catch (Exception e){
  throw new OtherException();
}

I'm hitting an exception in the try block, (which is then caught). How do I figure out where it's being thrown from?

Problems

  • The stack trace only shows the line in the catch block for the OtherException
  • Removing the try/catch block isn't straightforward, as there are many exceptions declared as thrown which are required to be caught in order for the code to compile.

It feels like there should be a straightforward way of doing this.

Note: I didn't write this code ;-)

like image 504
Tim Bellis Avatar asked Nov 27 '22 22:11

Tim Bellis


1 Answers

Use the cause parameter for Exceptions (see here):

try {

  //Lots of code. Seriously. Lots.

} catch (Exception e){
  throw new OtherException(e); // Trick is here
}

This way you get the cause exception as well in the stacktrace.

like image 153
rlegendi Avatar answered Nov 30 '22 23:11

rlegendi