CMPS 350 LAB 01: Exception Handling in C++

resources:
gdb basics
C++ stdexcept class
C++ exceptions FAQ
sample program
C++ tutorials (4C++ refresher)
"Opportunity is missed by most people because it is dressed in overalls and looks like work." - Thomas Alva Edison

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.

  1. range_error.h - This file contains the class definition and methods for the RangeError exception class. There are only three methods: the constructor, what(), and an overloaded output operator. A RangeError object data holds the source filename of the offending code, the line number in which the error occurs, and the faulty index value. The filename and the line number are grabbed from two compiler environmental variables: __FILE__ and __LINE__ (two underscores prepend and end the variables) when the RangeError object is instantiated.

  2. list.h - This file is a template class for an array based list. The first several methods in this class provide examples of constructing and tossing a RangeError object.

  3. lab01.cpp - In lab01.cpp several List objects are instantiated. There is one try block for error handling with a catch for RangeError objects and a generic catch block.

  4. Makefile - This Makefile will create an executable called 'lab01'. Simply type 'make' at the command line. You do not need to modify the Makefile.

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 lab01 
Copy the files for lab01 into your lab01 directory:
   $ cp /home/fac/melissa/public_html/cs350-f15/lab01_files/*  . ← this is a dot
Compile and execute the code.
   $ make
   $ ./lab01 10  # triggers one RangeError
   $ ./lab01 12  # aborts the program 
Task 1.
Add a fourth member to the RangeError class that will hold the name of the function in which the error was found. The function name is stored in the environment variable called __func__ (note: there are two underscores before and after func; earlier versions of g++ called this __FUNCTION__). You will need to modify two files: range_error.h and list.h. In range_error.h you must change the class definition, the overloaded output operator and the RangeError constructor to handle a fourth parameter (treat funcName the same as fileName). In list.h you need to modify all instances of the code in which a RangeError is instantiated to grab and pass the value of __func__.

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 107
HOW 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.