Java Reference
In-Depth Information
Self-Test Exercises (continued)
System.out.println("try block entered:");
number = 42;
if (number > 0)
throw new MyException("Hi Mom!");
System.out.println("Leaving try block.");
}
catch (MyException exceptionObject)
{
System.out.println(exceptionObject.getMessage());
}
System.out.println("End of example.");
The class MyException is on the CD that comes with this text.
15. Suppose that in Self-Test Exercise 14 the catch block were changed to the follow-
ing. (The type MyException is replaced with Exception .) How would this affect the
output?
extra code
on CD
catch (Exception exceptionObject)
{
System.out.println(exceptionObject.getMessage());
}
16. Suppose that in Self-Test Exercise 14 the line
number = 42;
were changed to
number = 58;
How would this affect the output?
17. Although an exception class normally carries only a string message, you can define
exception classes to carry a message of any type. For example, objects of the following
type can also carry a double “message” (as well as a string message):
public class DoubleException extends Exception
{
private double doubleMessage;
public DoubleException()
{
super ("DoubleException thrown!");
}
public DoubleException(String message)
{
super (message);
}
(continued)
Search WWH ::




Custom Search