Java Reference
In-Depth Information
Luckily, there are clues in the error message that can help you sort out where and why it
happened. First note the type of exception:
java.lang.ArithmeticException
. Already from
the name, you can see it must be something related to the calculations. Then, specifically it states
“/ by zero,” so somewhere your program has tried to divide by zero, an operation that's unde-
fined and therefore cannot be computed by Java. In such a small program, you can probably
easily locate where the division is occurring, but in case you need some direction, the error mes-
sage also shows where the error occurred:
chapter6.Errors.main(Errors.java:13)
. In other
words, line 13 in the main thread of the
Errors
class in the
chapter6
package. If you go to line
13, you'll see the program is trying to divide
retirementFund
by
yearsInRetirement
. If you
refer back to the initializations of these two variables, you'll find
retirementFund = 10000
and
yearsInRetirement = 0
. There's the problem! If you set
yearsInRetirement
to some other
value, say 20 years, for a person retiring at 65 and living to age 85, you will no longer have a divi-
sion by zero exception.
Division by zero is a classic runtime error example, because the syntax is correct and it is only
during runtime that a problem occurs. Other typical examples that you will no doubt encounter,
if you haven't already, include
null pointer exception
, index out of bounds exception, and file
not found exception. In the next section, you'll see explanations of the most common exceptions
beginners see.
identifying logical errors
If you've run the code that you just fixed, did you notice anything strange? There shouldn't be any
syntax or runtime errors, so your program should run normally. You should see
David Johnson
will have $41 per month for retirement.
printed to the console. At first glance, this seems
fine, but on further inspection, the number is lower than you would expect. In fact, 10,000 divided
by 20 years divided by 12 months is 41 dollars per month. But the program is supposed to be calcu-
lating an annual return rate of 10% for all the years between age 30 and age 65. So what's going on
here?
This is an example of a logical error. The code compiles and executes without error, but logically
produces the wrong result. Logical errors are perhaps the most difficult to spot, because there's
nothing to suggest there's an error unless you know what to expect and compare the actual results
to the expected results. If you are not careful to review what is happening, you may easily miss logi-
cal errors lurking in your code.
So first try to sort out where this program went wrong. We know the return rate is not being calcu-
lated correctly; this could be a problem in the
for
loop that is iterating each year or in the recalcu-
late method itself. You can start by testing one recalculate method call and see if it's working. Add
the following print statement to the
main
method to check this:
System.out.println("100 at 10%
annual interest is: " + recalculate(100,0.1));
But now, there's already a hint that something isn't right. See Figure 6-6.
The error hint tells you that you cannot add a
void
to a string. But the
recalculate
method
returns a
void
. You will have to change this. Since the method is calculating a
double
type, it
makes sense to make the method return a
double
.
Search WWH ::
Custom Search