Activity 10.6.1.
Try the
num
values 10 and 11 below to see this exception in action.Exception
class can be extended to handle cases that are not already covered by Java’s built-in exceptions. Exceptions that you define will be handled the same way by the Java interpreter, but you will have to throw
them yourself./**
* IntOutOfRangeException reports an exception when
* an integer exceeds its bound.
*/
public class IntOutOfRangeException extends Exception {
public IntOutOfRangeException (int Bound) {
super("The input value exceeds the bound " + Bound);
}
}
Exception
and consists entirely of a constructor method that calls the superclass constructor. The argument passed to the superclass constructor is the message that will be returned by getMessage()
when an instance of this exception is created.num
values 10 and 11 below to see this exception in action.IntField
class that we developed previously (Figure 10.5.6) so that it will only accept numbers that are less than a certain bound. First, let’s modify IntField
so that its bound can be set when an instance is created. We want its bound to be an instance variable with some initial value, and we want to provide a constructor that can be used to override the default (Figure 10.6.2).
IntField
:public class IntField extends JTextField {
private int bound = Integer.MAX_VALUE;
public IntField(int size, int max) {
super(size);
bound = max;
}
// The rest of the class is unchanged for now
} // IntField
IntField(int,int)
, which doesn’t duplicate any of JTextField
’s constructors. This is good design, because in extending a class, we want to be careful about the effect that our definitions have on the original methods in the superclass. Superclass methods should be overridden by design, not by accident. If a method is redefined inadvertently, it might not function as expected by users of the subclass.bound
. Integer.MAX_VALUE
is a class constant that sets the maximum value for the int
type. It’s an appropriate value to use, because any valid int
that the user types should be less than or equal to MAX_VALUE
. Given these changes to IntField
, let’s now incorporate our new exception into its getInt()
method (Listing 10.6.4).getInt()
throws an exception if the integer entered by the user is greater than the IntField
’s bound
. Here again, it is difficult to handle this exception appropriately in this method. The method would either have to return an erroneous value—because it must return something—or it must terminate. Neither is an acceptable alternative. It is far better to throw the exception to the calling method.IntFieldTester
class (Listing 10.6.5) provides a simple GUI interface to test the IntField
class. It prompts the user to type an integer that is less than 100, and then it echoes the user’s input. Note how the exception is handled in the actionPerformed()
method. If an exception is thrown in IntField.getInt()
, the actionPerformed()
method pops up an error dialog, and the erroneous input is not used. Instead, the user is given another chance to enter a valid integer.Exception
named FieldIsEmptyException
, which is meant to be thrown if the user forgets to enter a value into a IntField
.IntField.getInt()
method so that it throws and catches the FieldIsEmptyException
(copied from your solution above).