Solutions to Self-Study Exercises
|
Solution 10.2 | The unchecked exceptions are IndexOutOfBoundsException, NumberFormatException, and NullPointerException, because these are subclasses of RuntimeException. The others are checked exceptions. |
Solution 10.3 | An ArrayIndexOutOfBoundsException could be handled by the handlers in a, c, or d, because their classes are all superclasses of ArrayIndexOutOfBoundsException. |
Solution 10.4 | If Math.random() in MyClass2 returns 0.98 and then 0.44, the program will generate the following output: 0.98 is out of range Note that because the out-of-range error occurs in method1(), method2() is not called at all. |
Solution 10.5 | If Math.random() in MyClass2 returns 0.98 and then 0.44, the following stack trace would be printed: java.lang.ArithmeticException: 0.98 is out of range |
Solution 10.6 | If Math.random() in MyClass2 returns 0.44 and then 0.98, the program will generate the following output: Hello 0.44 |
Solution 10.7 | If Math.random() in MyClass2 returns 0.44 and then 0.98, the following stack trace would be printed: java.lang.ArithmeticException: 0.98 is out of range |
Solution 10.8 | The divide-by-zero error in BadDivide occurs in the expression n/d in Method2(). It would generate the following stack trace: java.lang.ArithmeticException: divide by zero |
Solution 10.9 | The following version of BadDivide.method2() will handle the divide-by-zero error itself: public void method2 (int n, int d) { |
Solution 10.10 | If someValue equals 1000, the code segment will print: Entering try block |
Solution 10.11 | If someValue equals 50, the code segment will print: Entering try block |
Solution 10.12 | try { |
Solution 10.13 |
|
Solution 10.14 | public class FieldIsEmptyException extends Exception { |
public int getInt() {
int num = 0;
try {
String data = getText();
if (data.equals(""))
throw new FieldIsEmptyException();
num = Integer.parseInt( getText() );
if (num > bound)
throw new IntOutOfRangeException(bound);
} catch (FieldIsEmptyException e) {
System.out.println("Error: " + e.getMessage() );
} catch (NumberFormatException e) {
System.out.println("Error: You must input an integer. Please try again.");
} catch (IntOutOfRangeException e) {
System.out.println(e.getMessage());
return 0;
}
return num;
}
No comments:
Post a Comment