13
if (true) {
    String a = "foo";
    String b = "bar";
}

If I set a breakpoint at String a = "foo"; eclipse will stop, and I can step over and see the value of a inside the variables window. But I can't step over the 2nd statement, it just leaves the code block and I never see the value of b.

This forces me to add a noop statement after String b = "bar"; just so I can see what b contains. I also can't add a breakpoint on the closing } which I think are maybe related issues.

I know Visual Studio allows this, so is there a way to do this in Eclipse?

Community
  • 1
  • 1
rtaranu
  • 131
  • 4
  • 1
    Are you absolutely positive you are debugging the same *version* of the class? For example, depending on your build tool, if you have multiple projects and your main is in another project than this source, it may be executing an old copy that has different lines of code – Bohemian Oct 16 '11 at 22:59
  • The above example has been tested and I'm sure of it, you can try it yourself. Set the breakpoint on `String a = "foo";` use F6 to step over, you'll see the value of `a`, use F6 again, and you'll jump out of the code block. You never get to see the value of `b` – rtaranu Oct 17 '11 at 01:18
  • Duplicate of http://stackoverflow.com/questions/7785887/cant-breakpoint-the-last-statement-of-a-code-block-in-eclipse – Stefan Feb 01 '17 at 17:49

4 Answers4

16

To set a breakpoint at the end of an arbitrary block is not possible (without byte-code hacking).

If it is a method body, then it is possible: you can set a Method breakpoint. Do this by double-clicking on the method definition line:

sample code with simple method breakpoint

(notice the little arrow?) and then in the breakpoints view, select the breakpoint to see both an Entry and an Exit option tick-box in the displayed properties:

modifying the breakpoint

The little arrow indicates that, by default, we have set a breakpoint on entry to the method.

Now select Exit (and deselect Entry) and you will see this in the breakpoints view:

exit breakpoint in breakpoints view

(There is a different little arrow, indicating an exit breakpoint.)

Now run the debugger on this little breaker ('Debug As a Java Application') and it will stop on the exit brace of the method:

debugger stopped

and the local variables (only a in this case) are now visible (with the correct values) in the Variables view:

variables just before exit

It is worth noticing that this type of breakpoint traps method exit however this happens -- even, for example, if we exit by throwing an exception.

Steve Powell
  • 25,354
  • 8
  • 41
  • 44
  • 1
    See [this answer](http://stackoverflow.com/questions/198041/eclipse-beakpoint-stop-before-leaving-a-java-method?rq=1) which I didn't find out until I looked :-( It is old advice, and mine is slightly different, being for Eclipse 3.7.2. – Steve Powell Oct 24 '12 at 16:47
  • In IntelliJ I am able to place break-points at the end of arbitrary code blocks (not necessarily methods). They must be doing the "byte code hacking" under the hood? – James Wierzba Jul 09 '15 at 15:13
4

You can highlight the expression on the right hand side of the assignment and press Ctrl+Shift+I (for 'inspect' I think). It will evaluate the expression and give you the result. That way you can know the value of b without needing a breakpoint after the assignment.

Gyan aka Gary Buyn
  • 12,242
  • 2
  • 23
  • 26
  • @rtaranu: I don't think it's a kludge, it seems very sensible to me. Since the debugger can't break on a `}`, and because the last assignment statement won't yet have executed when you break on it, it's very logical to just break at that point anyway and let the debugger evaluate the right-hand side of the expression for you. – Rob Oct 17 '11 at 02:04
  • @robjb: it's a kludge, because if the right hand side of the assignments has side-effects (function that modifies the state of the program), then it will get evaluated once so I can see the value, and then again once I let the program continue. So I modify the behaviour of the program when I inspect it. – rtaranu Oct 17 '11 at 03:07
  • @rtaranu: I would seriously hope the debugger is smart enough to push any changes onto its own stack, and pop them back off when it resumes control flow. Otherwise, how could such a feature work at all? – Rob Oct 17 '11 at 03:37
  • 1
    @robjb: actually the debugger is not that smart (I tested), and in many situations it can't. Ex: your right-hand side performs an IO operation (database / file / socket / whatever) that can't be poped off the stack. – rtaranu Oct 17 '11 at 23:12
  • It *is* a kludge -- see my answer. – Steve Powell Oct 25 '12 at 12:22
  • @robjb: If Eclipse cannot set a breakpoint on a `}`, that's a problem of Eclipse and not a technical limitation. Debuggers can very well break on a `}`, e.g. Visual Studio does. – Thomas Weller Sep 22 '15 at 07:37
-1

I'm honestly not sure what's going on in your Eclipse installation. I'm working in Java and just tried everything I can think of. I checked a static method for breakpoint on the last line that is not a } and it works just fine. I checked the same for a non-static method.

It breaks down this way: you can't breakpoint on a curly brace end, but it will default to whatever is under the curly brace, line-wise. So if you have a nested function:

public int DoesStuff(int x, int y) {
    if(x > y) {
        DoThing;
    }
    else {
        DoOtherThing;
    }
}

Then the last two braces cannot be break-pointed, but DoThing and DoOtherThing can. Make sense?

Rob
  • 5,223
  • 5
  • 41
  • 62
Duckies
  • 15
  • 5
  • I believe what you're saying is correct, but it doesn't answer the question. His problem seems to be that the last statement *before* the ending brace `}` cannot have a break-point and is skipped when stepping through. – Rob Oct 16 '11 at 23:18
  • I can break on `DoThing` or `DoOtherThing`, so that stops before the function executes. But if those functions return a value that is assigned ex: `int myInt = DoThing()` and I step over it, I can't see the value assigned to `myInt`. The next "break" will be outside the code block. – rtaranu Oct 17 '11 at 01:11
  • 1
    @Duckies Actually you *can* breakpoint on a method closing curly brace -- see my answer. – Steve Powell Oct 24 '12 at 16:42
-1

If ending statement is assignment and it is on local variable why do you want to see its value because it will not be in scope anymore and can't effect.

If it's setting same class attribute then you can see it when you return from this call and you have the object on which this method operated.

Although this doesn't answer the question but I am trying to understand the use-case of your problem.

gnat
  • 6,213
  • 108
  • 53
  • 73
Aman
  • 916
  • 11
  • 16
  • Use case is that I'm calling a poorly documented function in a 3rd party library and I want to see what it returns. Or I'm writing some code and I want to double check the value before I continue writing. – rtaranu Oct 29 '11 at 02:21
  • For the function that is in third party library, you can just evaluate the function call by putting watch or doing inspect. So when u r writing code ya this problem will be there :) . – Aman Oct 30 '11 at 06:27
  • It is not related to the scope of variables (well, a little, since the breakpoint should be set in a place before the cleanup has been done). If Eclipse cannot set a breakpoint on a `}`, that's a problem of Eclipse and not a technical limitation. Debuggers in general can very well break on a `}`, e.g. Visual Studio does. – Thomas Weller Sep 22 '15 at 07:39