DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 5.6 Running Until Encountering a Breakpoint

5.6.1 Problem

You don't want to single-step through many lines of code while debugging. Rather, you want to step through breakpoints only.

5.6.2 Solution

While paused at a breakpoint, click the Resume button in the Debug view or select Run Resume. Execution continues until the next breakpoint is encountered.

5.6.3 Discussion

If you don't want to keep single-stepping through your code, you have other options. For example, you can simply let your code execute until it reaches a breakpoint. To do that, just click the Resume button in the Debug view (the arrow button to the right of the word Debug in the Debug view), or select Run Resume.

In the previous recipe, we stopped at a breakpoint and then single-stepped to the next line of executable code. Clicking Resume resumes program execution until the breakpoint is encountered for a second time, and the index loopIndex will hold a value of 2, as shown in Figure 5-13.

Figure 5-13. Running to a breakpoint
figs/ecb_0513.gif


Clicking Resume again takes us to the next iteration, where loopIndex holds 1. Clicking loopIndex one more time makes the code execute until the program terminates, which is unexpected because we're waiting for loopIndex to equal 0.

Getting an unexpected result is an indication you've found your bug; the problem is in the line in which we've set up the for loop incorrectly:

public class DebugClass
{
    public static void main(String[] args)
    {
        for(int loopIndex = 3; loopIndex > 0; loopIndex--)
        {
            System.out.println(loopIndex + "...");
            if(loopIndex == 0)
            {
                System.out.println("Houston, we have liftoff.");
            }
        }
    }
}

The for loop should be written like this instead:

public class DebugClass
{
    public static void main(String[] args)
    {
        for(int loopIndex = 3; loopIndex >= 0; loopIndex--)
        {
            System.out.println(loopIndex + "...");
            if(loopIndex == 0)
            {
                System.out.println("Houston, we have liftoff.");
            }
        }
    }
}

That fixes the problem, as shown in Figure 5-14, where the debugged code operates as it should.

Figure 5-14. The working code
figs/ecb_0514.gif


In addition to resuming execution until a breakpoint is encountered, you also can select Run Run to Line. Just click a line and select this item; execution will continue to that line. This option is often extremely useful while debugging to skip over large sections of code and to set ad hoc breakpoints on the fly.

You also have another option. You can select Run Step Into Selection, which enables you to indicate where you want to step into. In the current line of execution, place the cursor on the name of a method that you want to step into, and click Step into Selection.

5.6.3.1 Eclipse 3.0

In Eclipse 3.0, the Step into Selection command is no longer restricted to the line currently executing (which was very annoying in previous versions of Eclipse).

5.6.4 See Also

Recipe 5.3 on setting up a debugging session; Recipe 5.4 on setting a breakpoint; Recipe 5.5 on stepping through your code; Recipe 5.7 on running until a specific line number is reached.

    Previous Section  < Day Day Up >  Next Section