Java Reference
In-Depth Information
Self-Test Exercises (continued)
System.out.println("Starting sampleMethod.");
if (test < 100)
throw new Exception();
}
}
The class Exercise is on the CD that comes with this text.
23. Suppose that in Self-Test Exercise 22, the line
extra code
on CD
sampleMethod(98.6);
in the try block is changed to
sampleMethod(212);
How would this affect the output?
24. Correct the following method definition by adding a suitable throws clause:
public static void doStuff( int n)
{
if (n < 0)
throw new Exception("Negative number.");
}
25. What happens if an exception is thrown inside a method invocation but the excep-
tion is not caught inside the method?
26. Suppose there is an invocation of method A inside of method B, and an invocation
of method B inside of method C. When method C is invoked, this leads to an invo-
cation of method B, and that in turn leads to an invocation of method A. Now, sup-
pose that method A throws an exception but does not catch it within A. Where
might the exception be caught? In B? In C? Outside of C?
9.3
More Programming Techniques for
Exception Handling
Only use this in exceptional circumstances.
WARREN PEACE, The Lieutenant's Tool
In this section, we present a number of the finer points about programming with
exception handling in Java.
Search WWH ::




Custom Search