2.17. Key Terms

 
[Page 58 ( continued )]

2.16. Debugging

In general, syntax errors are easy to find and easy to correct, because the compiler gives indications as to where the errors came from and why they are there. Runtime errors are not difficult to find either, since the Java interpreter displays them on the console when the program aborts. Finding logic errors, on the other hand, can be very challenging.

Logic errors are called bugs . The process of finding and correcting errors is called debugging . A common approach to debugging is to use a combination of methods to narrow down to the part of the program where the bug is located. You can hand-trace the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program. This approach might work for a short, simple program. But for a large, complex program, the most effective approach for debugging is to use a debugger utility.

JDK includes a command-line debugger (jdb), which is invoked with a class name . jdb is itself a Java program, running its own copy of Java interpreter. All the Java IDE tools, such as JBuilder, NetBeans, and Eclipse, include integrated debuggers . The debugger utilities let you follow the execution of a program. They vary from one system to another, but they all support most of the following helpful features:

  • Executing a single statement at a time : The debugger allows you to execute one statement at a time so that you can see the effect of each statement.

  • Tracing into or stepping over a method : If a method is being executed, you can ask the debugger to enter the method and execute one statement at a time in the method, or you can ask it to step over the entire method. You should step over the entire method if you know that the method works. For example, always step over system-supplied methods, such as System.out.println .


    [Page 59]
  • Setting breakpoints : You can also set a breakpoint at a specific statement. Your program pauses when it reaches a breakpoint and displays the line with the breakpoint. You can set as many breakpoints as you want. Breakpoints are particularly useful when you know where your programming error starts. You can set a breakpoint at that line and have the program execute until it reaches the breakpoint.

  • Displaying variables : The debugger lets you select several variables and display their values. As you trace through a program, the content of a variable is continuously updated.

  • Displaying call stacks : The debugger lets you trace all of the method calls and lists all pending methods. This feature is helpful when you need to see a large picture of the program-execution flow.

  • Modifying variables : Some debuggers enable you to modify the value of a variable when debugging. This is convenient when you want to test a program with different samples but do not want to leave the debugger.

Tip

If you use an IDE such as JBuilder, NetBeans, or Eclipse, please refer to Learning Java Effectively with JBuilder/NetBeans/Eclipse in the supplement on the Companion Website. The supplement shows you how to use a debugger to trace programs and how debugging can help in learning Java effectively.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net