views:

206

answers:

4

Hi,

How can I setup Eclipse to stop at the point an exception occurred.

I have an Eclipse breakpoint setup to break on an exception. In the code example below, the problem I'm having is Eclipse tries to open the Integer source code. Is there any way to just have debugger break at the point shown in my code example? If I move down the stack trace, I will get to this line, it'd be nice if there's a way to do this without the "Source not found" window coming up.

This can be done in Visual Studio, so it's driving me crazy not being able to find a way to do this in Eclipse.

package com.test;

public class QuickTest
{
  public static void main(String[] args)
  {
    try
    {
        test();
    }
    catch(NumberFormatException e)
    {
        System.out.println(e.getMessage());
    }
 }

  private static void test()
  {
    String str = "notAnumber";

    Integer.parseInt(str);//<----I want debugger to stop here
  }
}
+3  A: 
  1. Can't you just set a breakpoint on the Integer.parseInt(str) line?
  2. If you point your Eclipse project at using a JVM JDK instead of a JRE it should pick up the source code for all of the platform classes (Integer etc).
matt b
I'm trying to avoid opening the Integer class. I just want it to break on the problem with my code. Setting a breakpoint won't help as this is just a simple example. Real code has hundreds of classes, etc.
Chris Persichetti
You probably mean to say 'JDK' instead of 'JVM' (?)
Fortega
@Fortega, yes, thanks
matt b
@Chris How's setting the breakpoint won't help?
Cem Catikkas
Because I don't know where an exception will occur. I'm trying to get the debugger to stop on a line that causes an exception. I can't put a breakpoint everywhere where an exception might occur or I'd have thousands of them. In Visual Studio .Net I can set it up to stop on any exception. When I do this in Eclipse it wants to try to open up the source code where the exception occured.
Chris Persichetti
You know you can step through the code, right? You just put a breakpoint somewhere before you know where the exception is thrown and just move on to the next line and so on.
Cem Catikkas
@Chris, when that happens and you're effectively further down in the call stack then you wish to be, use what saugata suggested to be able to go back up the call stack to where you code called Integer.parseInt
matt b
Yeah that's what I figured :( hoping there was away around that. Guess I got spoiled with visual studio. Thanks for the input.
Chris Persichetti
+1  A: 

Preferences -> Java -> Debug -> Step Filtering Choose packages you want to filter out when debugging (java.*, sun.*, etc) On debug make sure the little step filtering arrow is selected.

This should do the trick.

Cool! I've always wanted to know how to do this!
charlie
Doesn't look like it works when a breakpoint is setup to break on exception though :(
Chris Persichetti
How about if you press step return, step over or drop to frame at that point? Not sure if it will work but it's a shot.
Yeah I tried it too and it doesn't seem to use step filters on exception breakpoints, only when stepping through debug mode. Your only option here may be to look in the stack trace in the debug window where it will have the exception thrown from Integer. Scroll down until you find your calling code that you want to examine and click on that line. It will bring up your source that you are interested in.
+2  A: 

The Debug view will show the entire stack of the current execution. You need to select the line you want to see. It will be same as you had a breakpoint there, you can see all the variables etc.

saugata
+4  A: 

I'm not sure how else to get there, but I just do:

Window -> Show View -> Breakpoints

And in that tab there is a "J!" that lets you set breakpoints on exceptions.

alt text

job
Yeah that's what I use. Except in my example it tries to open the Integer source code. I don't want the Integer source code to open. I just want the debugger to stop at the 'Integer.parseInt(str);'
Chris Persichetti
The debugger will stop when the exception is thrown (which is what you asked it to do) and will always open the editor at the source line that threw the exception - in this case in Integer.parseInt(). The whole call stack is also shown, and you can click on the appropriate level to look at the source at that level.
DJClayworth