Testing and Debugging
• Learn strategies for thoroughly testing programs.
• See some tips for debugging programs.
A bug is an error in a program. There are (at least) three kinds of bugs:
1. A function may not return the right value.
2. A method may incorrectly change data (for example, the values of static
or instance variables, or information in a file).
3. A method may throw an exception (the bottom of the page discusses
Testing is the process of analyzing and executing a program to determine
whether it has bugs. A test case is a set of inputs, together with the expected out-
put, that is used to test a section of a program, and a test is code that exercises a
Debugging is the process of locating a bug and removing it.
The strategies and hints provided in this chapter will help reduce your over-
all work. At first, it will feel like you are doing more work, but after you follow
these steps for several assignments, your productivity will increase, possibly dra-
What is an exception? When an abnormal event occurs, like division by 0 or an attempt to ref-
erence a field of a non-existent object (e.g. null .x ) occurs, Java throws an
exception , where an exception is an instance of class Throwable . Often, this
results in the display of an error message and termination of the program.
Section 10.1 explains the content of these errors messages, and the rest of Chap.
10 explains all the details of handling such exceptions.