Lab 10 - Midterm 2 solutions and Exception Handling
The midterm solutions can be found in the Solutions section.
Purpose: To learn how to use try, throw and catch in C++.
For this lab, we will be modifying the code from Lab 9
to use exception handling. Modify stack.h,
stack.cpp and your version of lab9main.cpp (or
the Solution Key version) to
throw and catch the following exceptions:
- For the pop function in the Stack class, it should throw an "EmptyStack"
exception class when you try to pop off an empty stack. All calls to
s.pop()
in main() should try the pop and catch any EmptyStack
exceptions that occur.
- For the push function in the Stack class, it should throw an "OutOfMemory"
exception class when it is unable to allocate a node using new. The push
function should try the new command and catch any "bad_alloc" or "exception"
class exceptions raised by the new command within the push function (these
are the standard exception class names in C++ for when new fails to allocate
memory). The catch within the push function for these C++ exceptions should
then throw an "OutOfMemory" exception class. Any calls to
s.push()
in main() should try the push and catch any OutOfMemory
exceptions that occur.
For both the EmptyStack and OutOfMemory exception classes, just define an
empty class in stack.h above the definition for stackNode.
Email your stack.h, stack.cpp and lab9main.cpp files to me