Page 632 - Introduction to Programming with Java: A Problem Solving Approach
P. 632
598 Chapter 14 Exception Handling
<meta name="ROBOTS" content="INDEX,FOLLOW" />
<meta name="Description" content="Park University offers
undergraduate, graduate, and online degree programs at campuses
nationwide. Park is accredited by the Higher Learning Commission
of the North Central Association of Colleges and Schools." />
...
7. [after §14.10] Multiple catch Blocks:
Suppose the code in a try block might throw any of the following exceptions: a) Exception
b) IllegalArgumentException
c) IOException
d) NumberFormatException e) RuntimeException
Identify an acceptable sequence for multiple catch blocks of these types.
8. [after §14.11] Correcting Problems:
Fix the problems in the NumberList program without making any changes to the NumberListDriver class.
If a user immediately enters “q” to quit, print “NaN” by making a small program correction that utilizes double’s NaN value, and avoid using the try-catch mechanism to catch the int arithmetic exception.
Sample session:
Enter a whole number (q to quit): q
Mean = NaN
*
*
*
*
public class TestExceptions
{
Apago PDF Enhancer
If the entry is not a “q,” and if it is not a legal integer, catch the exception, and in the catch block use the getClass method inherited from the Object class to print the name of the exception class followed by the error message with the statement:
System.out.println(e.getClass() + " " + e.getMessage());
Avoid the possibility of an ArrayIndexOutOfBoundsException by adding to the while condition size < numList.length, and perform the query and entry at the end of the while loop only if size < numList.length.
9. [after §14.12] TestExceptions:
What does this program output? Since this program converts between string and numeric values, use quotes
to denote string values.
/*************************************************************
TestExceptions.java
Dean & Dean
This looks up the value at a calculated index.
*************************************************************/
private double[] value =
new double[] {1.0, 0.97, 0.87, 0.7, 0.47, 0.17};
private int num;