eclipse beakpoint: stop before leaving a Java method
Asked Answered
M

3

20

Is there a way to tell the debugger to stop just before returning, on whichever statement exits from the method, be it return, exception, or fall out the bottom? I am inspired by the fact that the Java editor shows me all the places that my method can exit - it highlights them when you click on the return type of the method declaration, (Mark Occurrences enabled).

[eclipse 3.4]

Maple answered 13/10, 2008 at 15:44 Comment(0)
D
35

Put a breakpoint on the line of the method signature. That is where you write

public void myMethod() {

Then right-click on the breakpoint and select "Breakpoint Properties". At the bottom of the pop-up there are two checkboxes: "Method Entry", "Method Exit". Check the latter.

Doctor answered 13/10, 2008 at 16:41 Comment(1)
This seems to only work if myMethod() exits normally. The method exit breakpoint does not trigger when myMethod() is exited by a thrown exception.Nursery
C
3

You can set a method breakpoint.

Double click in the margin next to the method declaration. A breakpoint with an arrow decoration appears. Right-clicking to examine the properties, you can set "Suspend on:" for "Method Entry" and/or "Method Exit".

You can read more about them in the Eclipse Cookbook.

Conklin answered 13/10, 2008 at 17:12 Comment(0)
P
0

Good question. Off the top of my head, I'd do this:

public void method(Object stuff) {
    try {
        /* normal code */
    } finally {
        int x = 0;
    }
}

You can set the breakpoint on the x = 0 line, and it will ALWAYS be executed no matter where you return. Even with an exception being thrown, it will be run.

The catch to this is scope. Unless you define variables outside of the try block, you won't be able to see their values where you get to the finally block since they will have exited scope.

Having to just place 5 breakpoints (one for each return statement, whatever) may work best.

I hope there is a better way, I'd love to know it.

Papaveraceous answered 13/10, 2008 at 15:54 Comment(1)
I love this answer. Even though is requires a code change, it is debugger independent!Maple

© 2022 - 2024 — McMap. All rights reserved.