Java Reference
In-Depth Information
Display 9.7
Demonstration of How to Use
BadNumberException
(part 2 of 2)
Sample Dialogue 1
Enter one of the numbers 42 and 24:
42
Thank you for entering 42
End of program.
Sample Dialogue 2
Enter one of the numbers 42 and 24:
44
44 is not what I asked for.
End of program.
Self-Test Exercises
12. Defi ne 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.
13. Defi ne 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.
14. Suppose the exception class
ExerciseException
is defi ned 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.");
}
}