01debuggingajavaprogram
01debuggingajavaprogram
After you have deployed a Java program you must check that it compiles and runs correctly. A
program bug is an error in the program. Debugging is the process of finding and fixing these
errors.
Examples
doubel height; Misspelled keyword double
A run-time error is not caught by the compiler but causes the computer to abnormally halt
during the program's run.
Example
Entering input that is not a floating-point number causes this code to fail.
java.util.Scanner in = new java.util.Scanner( System.in );
System.out.print( "Enter the circle's radius: " );
double radius = in.nextDouble( );
A logic error is an error in the program's underlying logic so that the program output is incorrect.
Example
This statement incorrectly calculates the mileage on a car. There is no obvious error but the
program will output the wrong answer.
Example
Here is a typical Java diagnostic:
The third line tells you the class in which the error appears.
The fourth line reproduces the offending Java statement and the fifth line places a caret mark (^)
beneath the offending symbol.
Begin
Compile
program
Any
NO Done
diagnostics?
YES
NO
Fix the
NO FIRST one
Fix it
Does it
make sense?
The picture recommends that you fix the first error and recompile if the remaining errors don’t
make sense. This is because the remaining diagnostics may be spurious – a spurious diagnostic
identifies a correct statement as being incorrect. An avalanche of spurious diagnostics is called
error cascade.
Here’s why the compiler gives spurious diagnostics. It reads the program left to right, top to
bottom and generates a diagnostic if it encounters something unexpected. After this first error,
the compiler tries to recover by skipping the program’s text until reaching a symbol it
understands. If the skipped program text has information in it that the compiler needs to parse
subsequent statements, those statements will give diagnostics even though they are correct.
Example
The following application has been seeded with one error – the opening brace ({) at line 4 has
been removed. This single error results in 28 diagnostics issued by the compiler.
1 public class MPGx
2 {
3 public static void main( String [] args )
4
5 // declare data
6 double miles; // miles driven in car
7 double gallons; // number of gallons used
8 double mpg; // miles per gallon
9 // initialize data
10 miles = 341;
11 gallons = 15.5;
12 // calculate mpg
13 mpg = miles / gallons;
14 // output results
15 System.out.print ( miles + " mi. / " );
16 System.out.print ( gallons + " gal." );
17 System.out.println( " = " + mpg + " mpg" );
18 }
19 }
Example
Here is a Java run-time error message that results from entering input of the wrong data type:
The lines tell you exactly where the exception was thrown and how it was propagated back
through the chain of method calls. For example, the last line above shows:
at MPG.main(MPG.java:15)
Example
Suppose a program prints:
15.5
Since 0.045 22 , this output is clearly wrong.
341
You hunt down the error by comparing the program’s output to what it is supposed to output.
For the former, use a symbolic debugger to step through the program and view its internal
memory.
By comparing your worked answer to the debugger’s output, you can discover the exact
statement where the program begins to go wrong.
Exercises
1. Use jGRASP to enter, save and compile the MPGx application (page 4). Observe the error
cascade. Is the line number cited by the first diagnostic correct?
2. Use jGRASP to enter, save and compile the application below. What is the diagnostic?
What line number does it cite? Is the explanation accurate?
3. On line 5 change double to doubel. Recompile the application and observe the
diagnostic. Restore the application so that it is correct.
4. On line 8 change qty to Qty. Recompile the application and observe the diagnostic. What
does the diagnostic cannot find symbol mean? Restore the application so that it is
correct.
6. Practice debugging this program. It has approximately 10 compiler errors. Identify them all.