Java Reference
In-Depth Information
Self-Test Exercises
10. Define an exception class called
PowerFailureException
. The class should have a
constructor with no parameters. If an exception is thrown with this zero-argument
constructor,
getMessage
should return
"Power Failure!"
The class should also
have a constructor with a single parameter of type
String
. If an exception is
thrown with this constructor, then
getMessage
returns the value that was used as
an argument to the constructor.
11. Define an exception class called
TooMuchStuffException
. The class should have a
constructor with no parameters. If an exception is thrown with this zero-argument
constructor,
getMessage
should return
"Too much stuff!"
. The class should also
have a constructor with a single parameter of type
String
. If an exception is
thrown with this constructor, then
getMessage
returns the value that was used as
an argument to the constructor.
12. Suppose the exception class
ExerciseException
is defined as follows:
public class
ExerciseException
extends
Exception
{
public
ExerciseException()
{
super
("Exercise Exception thrown!");
System.out.println("Exception thrown.");
}
public
ExerciseException(String message)
{
super
(message);
System.out.println(
"ExerciseException invoked with an argument.");
}
}
What output would be produced by the following code (which is just an exercise
and not likely to occur in a program)?
ExerciseException e =
new
ExerciseException("Do Be Do");
System.out.println(e.getMessage());
extra code
on CD
The class
ExerciseException
is on the CD that comes with this text.
13. Suppose the exception class
TestException
is defined as follows:
public class
TestException
extends
Exception
{
public
TestException()
{
(continued)