10.6. Creating and Throwing Your Own ExceptionsLike other Java classes, the Exception class can be extended to handle cases 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. For example, Figure 10.20 shows the design of an exception that can be used for validating that an integer is less than or equal to a certain maximum value. It would be coded as follows: /** Figure 10.20. The IntOutOfRange exception.The class extends 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. Now let's consider an example where this new exception will be thrown. Suppose we wish to constrain the IntField class that we developed previously (Fig. 10.14) so that it will only accept numbers 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 (Fig. 10.21). Figure 10.21. The revised IntField class contains a bound on the size of the numbers that should be entered.This leads to the following revision of IntField: public class IntField extends JTextField { Our new constructor has the signature IntField(int,int), which does not duplicate any of JTextField's constructors. This is good design, because in extending a class, we want to be careful about the effect 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. Effective Design: Extending a Class
Note how we have handled the problem of setting the default value of the bound. Integer. MAX_VALUE is a class constant that sets the maximum value for the int type. It is 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 (Fig. 10.22). Figure 10.22. The revised IntField class containing the revised getInt() method.
This new version of 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 valuebecause it must return somethingor it must terminate. Neither is an acceptable alternative. It is far better to throw the exception to the calling method. The IntFieldTester class (Fig. 10.23) has the design and functionality shown in Figure 10.15. It provides a simple GUI interface to test the IntField class. It prompts the user to type an integer 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. Figure 10.23. An application that uses an IntField object to process integers.
Self-Study Exercises
|
Tuesday, December 15, 2009
Section 10.6. Creating and Throwing Your Own Exceptions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment