Lab 2 - Exception Handling
Due: Today at 5:00pm
This lab is worth 10 points.
Log into Helios and create a new directory for this lab. Copy genericlist.cpp
and lab1main.cpp from last week's lab to this new directory. Add the following
exception handling:
- bad_alloc detection in constructors - Take the current constructor
bodies and put each body within a try block. Catch the
bad_alloc
exception if it occurs and set the pointer to NULL and the size to 0.
- Index [] operator with InvalidIndex exception - Add the index
operator to the class. The index operator will throw an InvalidIndex object
if the index given is less than 0 or greater than size - 1. You will also need
to define the InvalidIndex class. To get the lab points, you only need to make
an empty exception class. However, if you wish to try more complex exception
classes, you can add an integer to the InvalidIndex class to record what the
invalid index was and a
string what()
function to print a message
about the exception.
- Call index operator from the main program - In run_tests, add a try
block that attempts to print out a[0]. This should trigger the InvalidIndex
exception since a is empty. Catch the InvalidIndex exception after the try
block.
Email me your new versions of genericlist.cpp and lab1main.cpp.