Java Reference
In-Depth Information
Self-Test Exercises
(continued)
public
MyException(String message)
{
super
("MyException: " + message);
}
}
What output would be produced by the following code (which is just an
exercise and not likely to occur in a program)?
int
number;
try
{
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 website that comes with this text.
extra code
on website
17. Suppose that in Self-Test Exercise 16 , the
catch
block were changed to the
following. (The type
MyException
is replaced with
Exception
.) How would
this affect the output?
catch
(Exception exceptionObject)
{
System.out.println(exceptionObject.getMessage());
}
18. Suppose that in Self-Test Exercise 16 , the line
number = 42;
were changed to
number = −58;
How would this affect the output?