Java Reference
In-Depth Information
next set of brackets (
responses[answer]
) is evaluated. That value is used as the
index for the
frequency
array to determine which counter to increment (in this
case,
frequency[1]
).
• The next time through the loop
answer
is
1
,
responses[answer]
is the value of
responses[1]
(that is,
2
—see line 9), so
frequency[responses[answer]]
is in-
terpreted as
frequency[2]
, causing
frequency[2]
to be incremented.
•W n
answer
is
2
,
responses[answer]
is the value of
responses[2]
(that is,
5
—
see line 9), so
frequency[responses[answer]]
is interpreted as
frequency[5]
,
causing
frequency[5]
to be incremented, and so on.
Regardless of the number of responses processed, only a six-element array (in which we ig-
nore element zero) is required to summarize the results, because all the correct response val-
ues are values from 1 to 5, and the index values for a six-element array are 0-5. In the
program's output, the
Frequency
column summarizes only 19 of the 20 values in the
re-
sponses
array—the last element of the array
responses
contains an (intentionally) incor-
rect response that was not counted. Section 7.5 discusses what happens when the program
in Fig. 7.8 encounters the invalid response (
14
) in the last element of array
responses
.
Response
An
exception
indicates a problem that occurs while a program executes. The name “excep-
tion” suggests that the problem occurs infrequently—if the “rule” is that a statement normal-
ly executes correctly, then the problem represents the “exception to the rule.”
Exception
handling
helps you create
fault-tolerant programs
that can resolve (or handle) exceptions.
In many cases, this allows a program to continue executing as if no problems were encoun-
tered. For example, the
StudentPoll
application still displays results (Fig. 7.8), even though
one of the responses was out of range. More severe problems might prevent a program from
continuing normal execution, instead requiring the program to notify the user of the prob-
lem, then terminate. When the JVM or a method detects a problem, such as an invalid array
index or an invalid method argument, it
throws
an exception—that is, an exception occurs.
Methods in your own classes can also throw exceptions, as you'll learn in Chapter 8.
To handle an exception, place any code that might throw an exception in a
try
statement
(lines 17-26). The
try
block
(lines 17-20) contains the code that might
throw
an excep-
tion, and the
catch
block
(lines 21-26) contains the code that
handles
the exception if one
occurs. You can have
many
catch
blocks to handle different
types
of exceptions that might
be thrown in the corresponding
try
block. When line 19 correctly increments an element
of the
frequency
array, lines 21-26 are ignored. The braces that delimit the bodies of the
try
and
catch
blocks are required.
When the program encounters the invalid value
14
in the
responses
array, it attempts to
add
1
to
frequency[14]
, which is
outside
the bounds of the array—the
frequency
array