BIG C++ Cay Horstmann & Timothy Budd
Laboratory Notebook Chapter 19 – Exception Handling
Your name:
Your email address:
Your student ID number:
Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.
To gain experience with
· exceptional conditions that may arise within a program
· making your programs more reliable and robust by dealing with exceptional conditions
· using a variety of mechanisms to handle exceptional conditions
· using try blocks and catch handlers in your own programs
Submit all of the code for your answer in the space below.
In the space below, modify the function such that it throws an object of type InvalidInputException in the case that either one or both inputs are invalid. Do not bother implementing this object.
Implement a main function in the space below such that, enclosed within a try statement, the computeAge() function is executed with invalid inputs. A catch statement should catch the exception, print out an error message, and cause the program to immediately exit.
Finally, define and implement the InvalidInputException class such that it stores a single text error message initialized by the class’s constructor.
P3. Creating a Working Program
Now that you have created the code for P1 and P2, you can assemble it together into a working program. Construct a main() function that prompts the user for the name of a file. Upon the user’s entry, the program should construct the vector of LotteryDrawing objects and print each drawing to the screen.
In the space provided, rewrite the readDrawing() function such that it throws a pointer to a BadDrawingException object in the case of an error. For now, assume an error occurs only when a number read from the specified file is negative. Do not yet implement this exception class.
Finally, modify the readDrawing() class such that it throws the correct exception type, upon encountering an exception, in the space below. This task should be relatively easy as it is an extension to R3.
Did you choose to have your BadDrawingException base class derive any of its functionality from a standard exception class? Why or why not?
R4. Missed the Throw
Run the program written thus far and modify the input file such that an invalid entry is present. The readDrawing() function will throw an exception. Record the output of the program in the space below.
Why did you get this output?
P5. Catch It
Now that you have observed what happens whenever a program throws an object that is not caught, rewrite your createLotteryHistory() function such that it catches all possible exceptions from the readDrawing() function. Upon encountering an exception, the createLotteryHistory() function should catch it, close the file, and re-throw the exception. Provide your code in the space below.
Do not forget to send your answers when you are finished.