OOP 6. Validity Checking - TimeOfDay
- Show how constructor can guarantee legal initial values.
- Throwing an IllegalArgumentException.
A constructor can ensure good values
By defining a constructor, all TimeOfDay objects must be created with your constructor. This gives you control over how parameters are used, and most importantly it can prevent the creation of illegal TimeOfDay values.
What to do if there are errors. Errors can be grouped into two general classes:
- User errors. If the user makes an error, the user interface code must tell them what they did wrong, how to correct it, etc. This is an issue for the user interface code, not for your basic value objects. Either the user interface should check for values before passing them to the model code, or the model code should have some way of informing the user interface that there was an error, typically by throwing an exception.
- Programmer errors. You should be very unforgiving of programmer errors discovered during testing There are several ways to handle errors (see Error processing). One of the best ways to handle bad values is to throw an exception (see Throwing exceptions) to stop execution if an illegal situation is encountered.
Crash please. The TimeOfDay class is called by a programmer, not the user. If illegal values are passed to the constructor, it's because the programmer isn't doing their job, so the program should be stopped immediately.
The program below should crash because an illegal number of minutes is passed to the second call on the constructor. The result would be an error message something like the following.
Exception in thread "main" java.lang.IllegalArgumentException: TimeOfDay: Bad constructor value: 14:99 at TimeOfDay2a.<init>(TimeOfDay2a.java:16) at TimeTest2a.main(TimeTest2a.java:27)
TimeOfDay class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// File : oop/timeofday/TimeOfDay2a.java
// Purpose: A 24 hour time-of-day class to demo intro OOP concepts.
// Author : Fred Swartz
// Date : 2009-09-18
// Issues : Validity checking.
public class TimeOfDay2a {
//========================================= instance variables
public int hour;
public int minute;
//================================================ constructor
public TimeOfDay2a(int h, int m) {
//... Check values for validity.
if (h < 0 || h > 23 || m < 0 || m > 59) {
throw new IllegalArgumentException(
"TimeOfDay: Bad constructor value: " + h + ":" + m);
}
hour = h;
minute = m;
}
}
|
Test program source
The following program will produce a dialog box indicating that an exception was correctly thrown, then it will produce an error on the console (if run from the console) because the exception was not caught.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// File : oop/timeofday/TimeTest2a.java // Purpose: Test TimeOfDay2a exception throwing.. // Author : Fred Swartz - 2006-09-18 - Placed in public domain. // Issues : When you run this test, it should produce an error. import javax.swing.*; public class TimeTest2a { public static void main(String[] args) { //... First constructor call is OK try { TimeOfDay2a now = new TimeOfDay2a(8 , 35); // OK } catch (IllegalArgumentException iax) { JOptionPane.showMessageDialog(null, "Failure: " + iax); } //... Constructor should fail. try { TimeOfDay2a never = new TimeOfDay2a(14, 88); // ILLEGAL VALUE. } catch (IllegalArgumentException iax) { JOptionPane.showMessageDialog(null, "Success: " + iax); } //... Exception gets passed up to runtime environment to console. TimeOfDay2a never = new TimeOfDay2a(14, 99); // ILLEGAL VALUE. } } |