Java Reference
In-Depth Information
26. It might be caught in method B. If it is not caught in method B, it might be
caught in method C. If it is not caught in method C, it might be caught outside
of method C.
27. Yes, you can have a
try
block and corresponding
catch
blocks inside another
larger
try
block.
28. Yes, you can have a
try
block and corresponding
catch
blocks inside another
larger
catch
block.
29.
In finally block.
Caught in main.
If the argument to
sampleMethod
were
−
42
instead of
42
, the output would be
Caught in sampleMethod.
In finally block.
After finally block.
30.
Assuming the first item input is not a correctly formed
int
value, the program
will go into an infinite loop after reading the first item input. The screen will
continually output a prompt for an input number. The problem is that unless
the new-line symbol
'\n'
is read, the program will continue to try to read on the
first input line and so continually reads in the empty string.
31.
The following is the method definition embedded in a test program. This pro-
gram would give the same dialogue as the one in Display 9.11. The program is
included on the CD that accompanies this topic.
extra code
on CD
import
java.util.Scanner;
import
java.util.InputMismatchException;
public class
getIntDemo
{
/**
Precondition: keyboard is an object of the class Scanner that has
been set up for keyboard input (as we have been doing right
along).
Returns: An int value entered at the keyboard.
If the user enters an incorrectly formed input, she or he is
prompted to reenter the value,
*/
public static int
getInt(Scanner keyboard)
{
int
number = 0;
//to keep compiler happy
boolean
done =
false
;
while
(! done)
{