
Date_Error: EXCEPTION;
and in the function body, before you proceed to compute the day of the week, add some tests to determine whether the date is valid. For example, you could write
CASE Month IS
WHEN 4 | 6 | 9 | 11 =>
IF Day = 31 THEN
RAISE Date_Error;
END IF;
WHEN 2 =>
-- check for leap year, etc.
...
END CASE;
Test this modified function, perhaps using your Project 3 program as a starting point.
WITH Ada.Calendar; PACKAGE DayWeek.IO IS TYPE Months IS (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec); PROCEDURE Get (MonthName: OUT Months; Day: OUT Ada.Calendar.Day_Number; Year: OUT Ada.Calendar.Year_Number); -- Pre: None -- Post: The 3 values MonthName, Day, Year represent a valid date END DayWeek.IO;In the body of this package, the procedure Get will read a date robustly,that is,
DayCode: DaysOfWeek;
then call DayWeek.DayOfWeek with the 3 parameters received by Get from the user.
DayCode := DayWeek.DayOfWeek(Month => Month, Day => Day, Year => Year);The function call will either return a value to DayCode, or raise the exception Date_Error. So now you can put an exception loop in the body of Get whose handler looks like
EXCEPTION WHEN DayWeek.Date_Error => -- write message to userYou can use a modification of your program from Project 3 to test this child package. Since the month abbreviations are now declared in the package, you will need to remove the declaration from your Project 3 program. Also, instead of reading the month, day, and year directly in this program, just call DayWeek.IO.Get.