Time Class Member Function setTime and Throwing Exceptions

Function setTime (lines 17–30) is a public function that declares three int parameters and uses them to set the time. Lines 20–21 test each argument to determine whether the value is in range, and, if so, lines 23–25 assign the values to the hour, minute and second data members. The hour value must be greater than or equal to 0 and less than 24, because universal-time format represents hours as integers from 0 to 23 (e.g., 1 PM is hour 13 and 11 PM is hour 23; midnight is hour 0 and noon is hour 12). Similarly, both minute and second must be greater than or equal to 0 and less than 60. For values outside these ranges, setTime throws an exception (lines 28–29) of type invalid_argument (from header <stdexcept>), which notifies the client code that an invalid argument was received. As you learned in Section 7.10, you can use try...catch to catch exceptions and attempt to recover from them, which we’ll do in Fig. 9.3. The throw statement (lines 28–29) creates a new object of type invalid_argument. The parentheses following the class name indicate a call to the invalid_argument constructor that allows us to specify a custom error message string. After the exception object is created, the throw statement immediately terminates function setTime and the exception is returned to the code that attempted to set the time.


 1   // Fig. 9.3: fig09_03.cpp
 2   // Program to test class Time.                    
 3   // NOTE: This file must be compiled with Time.cpp.
 4   #include <iostream>
 5   #include <stdexcept> // for invalid_argument exception class
 6   #include "Time.h" // include definition of class Time from Time.h
 7   using namespace std;
 8
 9   int main()
10   {
11      Time t; // instantiate object t of class Time
12
13      // output Time object t's initial values
14      cout << "The initial universal time is ";
15      t.printUniversal(); // 00:00:00
16      cout << " The initial standard time is ";
17      t.printStandard(); // 12:00:00 AM
18
19      t.setTime( 13, 27, 6 ); // change time
20
21      // output Time object t's new values
22      cout << " Universal time after setTime is ";
23      t.printUniversal(); // 13:27:06
24      cout << " Standard time after setTime is ";
25      t.printStandard(); // 1:27:06 PM
26
27      // attempt to set the time with invalid values
28      try
29      {
30         t.setTime( 99, 99, 99 ); // all values out of range
31      } // end try
32      catch ( invalid_argument &e )
33      {
34         cout << "Exception: " << e.what() << endl;
35      } // end catch
36
37      // output t's values after specifying invalid values
38      cout << " After attempting invalid settings:"
39         << " Universal time: ";
40      t.printUniversal(); // 13:27:06
41      cout << " Standard time: ";
42      t.printStandard(); // 1:27:06 PM
43      cout << endl;
44   } // end main


The initial universal time is 00:00:00
The initial standard time is 12:00:00 AM

Universal time after setTime is 13:27:06
Standard time after setTime is 1:27:06 PM

Exception: hour, minute and/or second was out of range

After attempting invalid settings:
Universal time: 13:27:06
Standard time: 1:27:06 PM


Fig. 9.3. Program to test class Time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.197.164