4

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 ;-)

2
  • Answer: I set a breakpoint in the catch block and examined the Exception 'e'. Its call stack told me where it was thrown. Commented May 24, 2012 at 16:06
  • Debugging with breakpoints works wonders. :-) Commented May 24, 2012 at 16:15

6 Answers 6

14

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.

Sign up to request clarification or add additional context in comments.

Comments

4

You can use throw new OtherException(e);. As the documentation explains, this constructor constructs a new exception with the specified cause.

Comments

4

In Eclipse, you can set a breakpoint triggered by an exception. See Add Java Exception Breakpoint.

For this particular case, you'll need to ensure that "Suspend on caught exceptions" is ticked.

Once Eclipse breaks into the debugger, you'll have a lot of tools at your disposal. You'll see the call stack, will be able to examine variables etc.

Comments

2

Just print stacktrace or run on debug mode

e.printStackTrace()

2 Comments

Better to log it than print a stack trace.
I see, but sometime loggin is unnecessary if you want to debug the error only.
2

Pass the exception e in your OtherException constructor when throwing it. It will give you the complete stack trace with the exact line throwing the exception:

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

If OtherException doesn't have a constructor that takes an Exception or Throwable you could do:

catch (Exception e) {
    OtherException o = new OtherException();
    o.initCause(e);
    throw o;
}

Comments

0

You can also try printing out the error message to the console: System.out.println(e.getMessage());

Breakpoints are very useful though, since you can then trace through the code and see exactly when it gets to the catch block.

1 Comment

Better to log it than print a stack trace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.