Java Reference
In-Depth Information
Self-Test Exercises
(continued)
{
System.out.println("Trying");
sampleMethod(98.6);
System.out.println("Trying after call.");
}
catch
(Exception e)
{
System.out.println("Catching.");
}
System.out.println("End program.");
}
public static void
sampleMethod(
double
test)
throws
Exception
{
System.out.println("Starting sampleMethod.");
if
(test < 100)
throw new
Exception();
}
}
extra code
on website
The class
Exercise
is on the website that comes with this text.
25.
Suppose that in Self-Test Exercise 22 , the line
sampleMethod(98.6);
in the
try
block is changed to
sampleMethod(212);
How would this affect the output?
26. 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.");
}
27. What happens if an exception is thrown inside a method invocation, but the
exception is not caught inside the method?
28. 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 invocation of method B, and that in turn leads to an invocation of
method A. Now, suppose 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?