Exercise 35
Info

This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions if you get stuck. Aim to complete as much as possible during our meeting. You are encouraged to work at home to complete what you do not get through today, and ask questions over Piazza or in office hours.

Learning Objectives

This exercise is designed to be an introduction to throwing and catching exceptions in C++. As you work on this exercise, you may find the following list of standard C++ exceptions useful: http://en.cppreference.com/w/cpp/error/exception.

Part 1

Pull the starter code for this exercise from the public repo by taking the following steps:

  1. Log into ugrad, then navigate to the public repo and type git pull to synchronize your local working copy with the remote repo.

  2. Copy the above files from the public repository (in the /exercises/ex35/ directory) into your personal repo where desired. Then navigate to your personal repo folder for this exercise. Confirm that you can see the starter files for today’s exercise – files named exceptionExercise.cpp, goodData.txt, tooManyInts.txt, and nonIntData.txt.

Part 2

Compile exceptionExercise.cpp and run the program to see how the existing version behaves. Notice that it expects an input filename as a command-line argument, so try running it with goodData.txt as input. In the remaining parts, we will consider various types of error conditions the program might encounter and try to deal with them gracefully.

Part 3

What if the file contains more than ten integers? Execute the program using tooManyInts.txt and watch what happens. You can see that the program crashes with a particular exception condition. Add a try/catch block in main that can handle the situation and print a useful error message.

Part 4

What if the user specifies the name of a file that does not exist? Execute the program and type in notAFile.txt (the name of a file that does not exist) and watch what happens. Edit exceptionExercise.cpp to handle this more gracefully. In particular, it would be useful to throw an exception from within readFile if you detect such a situation.

Tip

The method called is_open() can be helpful here.

Then add a try/catch structure in main that can handle the situation and print a useful error message. If your catch block catches an exception called e, it might be useful to output e.what() to give the user additional information.

Tip

It might be a good idea to throw and catch an std::ios_base::failure type exception in a situation like this.

Part 5

What if the input file contains something other than ints? Execute the program with nonIntData.txt and watch what happens. Add a try/catch structure that can handle the situation and print a useful error message.

Part 6 (optional)

If you have time, add a try/catch block to the readFile() function so that if we try to go past the end of the array, the exception will be caught and things will be “fixed” (by re-sizing the array so it will hold the new element and then re-adding it).

Reminder

Remember to add and commit to your local repo copy as your work. Push to your remote repo when finished and submit to Gradescope to check your solution. Use exit to logout from your ugrad account when finished. If you continue to work on the program after class, make sure to keep your repo updated as well!