Post date: Nov 16, 2017 6:30:10 AM
Chapter 9: Exception Handling
9.1 Exception handling Basics
9.2 Throwing Exception in Methods
Recommended exercises from the book: Programming Exercise 6 and 7!
Lab Exercises:
Ex 1: Write a program that calculates the average of N integers. The program should prompt the user to enter the value of N and then afterward must enter all N numbers. If the use enters a non-positive value of N, then an exception should be thrown (and caught) with the message "N must be positive." If there is any exception as the user is entering the N numbers, an error message should be displayed, and the user prompted to enter the number again.
Ex 2: Write a program that converts dates from numerical day/month/year format to normal "day month year" format (for example: 01/01/2015 corresponds to 1 January 2015). You will define three exception classes, one called DayException, another called MonthException, and a third called YearException. If the user enters anything other than a legal month number (integer from 1 to 12), your program will throw and catch a MonthException and ask the user to reenter the month. Similarly, if the user enters anything other than a valid day number (integers from 1 to either 28, 29, 30, or 31, depending on the month and year), then your program will throw and catch a DayException and ask the user to reenter the day. If the user enters a year that is not in the range 1000 to 3000 (inclusive), then your program will throw and catch a YearException and ask the user to reenter the year. (There is nothing very special about the numbers 1000 and 3000 other than giving a good range of likely dates.)