In this lab you will learn how to recover from fatal errors that occur at runtime. Under Unix, fatal errors will immediately terminate the program. As a programmer, your job is to prevent this from occuring by adding some form of error/exception handling mechanism to trap all critical errors. Critical errors include divide by zero, attempting to access memory beyond the bounds of an array or dereferencing a null or dangling pointer. Primitive error handling can be as simple as displaying a message to standard error (stderr, cerr), standard out (stdout, cout) or to a log file. You then choose whether to continue or abort the program. Most modern object-oriented languages are taking the approach that error handling should be built-in to the language. Built-in exception handling features provide a standardized and much more sophisticated method of handling errors.
C++ has built-in error handling called exception classes. C++ also supports user-defined exception classes. In this lab you will modify a user-defined exception handling class called RangeError. The class is designed to return out-of-range errors in a list template class. The code is here on sleipnir. Description of the files.
To understand C++'s exception handing facilities, you must know the program flow of try, catch, and throw. In particular, the code that is executed and the code that is skipped within and without try and catch blocks. You should be able to answer these questions after completing this lab.
LAB 01 INSTRUCTIONS
Task 0.
Create the following subdirectory to hold your 350 lab01 files:
$ cd # change to your home directory $ mkdir cs350 $ chmod 700 cs350 # restrict permissions to protect your files $ cd cs350 $ mkdir lab01 $ cd lab01Copy the files for lab01 into your lab01 directory:
$ cp /home/fac/melissa/public_html/cs350-f15/lab01_files/* . ← this is a dotCompile and execute the code.
$ make $ ./lab01 10 # triggers one RangeError $ ./lab01 12 # aborts the programTask 1.
Task 2.
Next, open a file named log for writing error messages to.
Redirect stderr (cerr) to log.
See this sample program for
the code to do this.
Task 3.
Your third task is to catch all range out-of-bound errors in lab01.cpp
that may be thrown from list.h.
There is only one try block in the original lab01.cpp. Any errors that
are tossed to main() that are not caught inside a try block
will result in immediate
termination (which happens if you pass a value > 10 to the program
from the cmdline.)
Modify lab01.cpp with additional try blocks so that termination
will not happen and every exception spits an error to the log file.
Review main.cpp for an
example for how to write multiple try blocks. If all is working
the output from your
code should look like this:
$ ./lab01 12 Santa Claus Lab01 Size: 12 =================================== Sorted integer List of size 10 6 7 9 23 23 37 37 50 50 51 Sorted float List of size 10 2.3 4.7 6.0 8.0 8.3 10.0 11.3 11.3 13.7 17.3 Swap first and last elements in fList Testing implicit copy constructor and assignment operator. 2.3 4.7 6.0 8.0 8.3 10.0 11.3 11.3 13.7 17.3 2.3 4.7 6.0 8.0 8.3 10.0 11.3 11.3 13.7 17.3 Testing range error conditions: swap(-1,size-1) Testing code after catch int file lab01.cpp, line 117 $ cat log Name: Santa Claus, LAB 01, Log file =================================== RangeError exception thrown in list.h, function: add, lineno: 55, index: 10 RangeError caught in add, file: lab01.cpp, line: 61 RangeError exception thrown in list.h, function: swap, lineno: 105, index: 11 RangeError caught in swap(), file lab01.cpp, line: 81 RangeError exception thrown in list.h, function: max, lineno: 125, index: 10 RangeError caught in max, file lab01.cpp at line 91 RangeError exception thrown in list.h, function: swap, lineno: 105, index: 11 RangeError caught in swap, file lab01.cpp, lineno 107HOW YOUR CODE WILL BE GRADED
You do not need to email anything to me. Your code must reside in this subdirectory (see instructions above):
/home/stu/{username}/cs350/lab01/Make sure you keep permissions for group on your home directory to read + execute or my script will have a little trouble. You will be marked off -1 pt if your files are not protected! I will copy your code from your directory after the due date, compile and execute your code. Your output does not have to look exactly like mine just similar - I review the output manually.