Wednesday, December 10, 2008

Debugging a Java

Debugging a Java Program

No matter how carefully you plan your programs, sooner or later you encounter bugs. You need to watch out for basically two kinds of bugs:

  • Incorrect results, such as a program that's supposed to calculate test scores but gives you a C when you score 99 out of 100 or a program that's supposed to calculate sales tax of 5% but that says the sales tax on a $29.95 purchase is $149.75 instead of $1.50.

  • Program crashes, such as when a program that's supposed to divide one number into another and print the answer instead prints out this message:

         Exception in thread "main" java.lang.ArithmeticException: / by zero
    at BugApp.main(BugApp.java:19)

Stepping through your programs

One of the most basic skills for debugging is executing program statements one at a time. This is called stepping, and it can be a very useful debugging technique. By stepping through your program one statement at a time, you can view the effects of each statement and identify the source of errors. Sometimes just knowing which statements are being executed is all you need to determine why your program isn't working.

In Eclipse, the Debug View section of the Debug perspective is where you control the execution of the program you're debugging. This view displays a tree that indicates each of the threads in your program. (If you don't know what threads are, don't worry about it. Most console-based programs, such as the BugApp program shown in Figure 4-11, use only one thread apiece anyway. You find out how to code programs that use more than one thread in Book V.)

Before you can control the execution of a thread, you must first suspend the thread so that its statements stop executing. In general, you can suspend a thread for debugging three ways:

  • When an unhandled exception occurs, the thread is automatically suspended. In Figure 4-11, the BugApp program's main method is suspended because a divide-by-zero exception has occurred and the program didn't catch it. If your program is throwing an exception that you don't expect, you can simply debug the program and allow the exception to suspend the thread. Then you can try to track down the cause of the problem.

  • Before you debug the program, you can set a breakpoint at any statement in the program. Then, when execution reaches that statement, the thread is suspended. To set a breakpoint, simply double-click the left margin of the Java editor next to the statement where you want the thread to be suspended.

    Image from book If a long-running thread is in a loop, you can suspend it by clicking the thread in the Debug View window and clicking the Suspend button (shown in the margin).

When you suspend a thread, the statement that will be executed next is highlighted in the Java editor. Then, you can continue the thread's execution one or more statements at a time by clicking the buttons at the top of the Debug View. Table 4-1 describes the most commonly used buttons.

Table 4-1: Commonly Used Buttons
Open table as spreadsheet

Button

Name

Description

Image from book

Resume

Resumes execution with the next statement. The thread continues executing until it is suspended by an uncaught exception or a breakpoint.

Image from book

Terminate

Terminates the thread.

Image from book

Step Into

Executes the highlighted statement, and then suspends the thread.

Image from book

Step Over

Skips the highlighted statement and executes the next statement, and then suspends the thread.

Image from book

Run to Return

Executes the highlighted statement and continues executing statements until the end of the current method is reached. Then, the thread is suspended.

Examining variables

When a thread is suspended, you can examine its variables to see if they're set to the values you expect. In many cases, you can discover programming errors. For example, if you think a variable named customerFirstName should contain a customer's first name and instead it contains the name of the state in which the customer lives, you can conclude that you didn't assign the variable's value properly. (Of course, this might be ambiguous if the customer happens to be named Indiana Jones.)


Tip

The easiest way to examine the value of a variable is to simply point the mouse at the variable in the Java editor. For example, Figure 4-12 shows how the value of the variable i appears when you hover the mouse pointer over it. Here, the pop-up message indicates that the variable i has a value of 0. (This message might be a clue as to why the program has thrown a divide by zero exception.)


Figure 4-12: Displaying a variable value.

You can also inspect variables by using the Variables View, as shown in Figure 4-13. Each variable is listed on a separate line in the top part of the Variables View. In addition, the bottom part (called the Detail pane) displays the value of the currently selected variable. Note that as you step through the various statements in your program, variables appear in the Variables View as they are declared and they disappear from view when they go out of scope.


Figure 4-13: The Variables View shows the value of each variable.

Setting breakpoints

A breakpoint is a line in your program where you want the program to be suspended. Setting a breakpoint allows you to efficiently execute the portions of your program that are working properly, while stopping the program when it reaches the lines you believe to be in error.

All the breakpoints in your program are listed in Breakpoints View, as shown in Figure 4-14. The following paragraphs describe some of the ways you can work with breakpoints in this view:

  • The check box next to each breakpoint indicates whether the breakpoint is enabled. Execution is suspended at a breakpoint only if the breakpoint is enabled.

  • You can delete a checkpoint by clicking the breakpoint to select it, and then pressing the Delete key or clicking the Remove Selected button (shown in the margin).

  • You can remove all the breakpoints you've set by clicking the Remove All Breakpoints button (shown in the margin).

  • If you double-click a breakpoint in Breakpoint View, the Java Editor window scrolls to the line at which the breakpoint is set.


  • Tip

    If you want the program to be suspended only after it has hit the breakpoint a certain number of times, right-click the breakpoint and choose Hit Count from the shortcut menu that appears. Then enter the number of times you want the statement to execute before suspending the program, and click OK.


Figure 4-14: The Breakpoints View is where you control breakpoints

No comments:

Post a Comment