UMBC CMSC 202 Computer Science II

Lab12: Exceptions


Objective

In this lab you will convert our traditional exit(1) error checking with exceptions.

Assignment: Change the current error checking to use exceptions

You are given an IntStack class that has plenty of error checking in it. However, whenever you do something it doesn't like, it exits the program.

This is bad because you don't want IntStack to handle the problem... YOU want to handle the problem.

The general rule is that only main should decide if the program should exit... nowhere else. If main can't handle the problem, nobody can. For example, if the stack is empty when we pop, main may know a way to handle it and can avoid exiting.

Step 1: Get the files

The only file you need for this lab is exceptions.cpp. This file contains the main and the IntStack Class.

The IntStack class works in the following way:
When constructed, it allocates a vector to have a certain size. This size will not change and limits the amount of items that can go in the stack. IntStack also has two functions that are usually included with stacks: push and pop. Push adds an item to the stack; this can error if the stack doesn't have any more room. Pop removes the most recently added item; this can error if the stack is empty. The stack keeps track of the top with an unsigned int called cur_index. This number stores the index that will be pushed onto next. For example, if cur_index is 0, the stack is empty and the next pushed item will be placed in index 0 of the vector.

Main works in the following way:
There are three tests that specifically trigger each error case.

Step 2: Handle the constructor's error case

The constructor errors when a negative size is passed in. Write a InvalideSize class to represent this error at the top of the exceptions.cpp file. This exception should take a string in its constructor and have a GetMessage() accessor for this string. throw this exception with an error message passed to the exception's constructor, instead of printing an error message and doing exit(1). Write the code in main to catch the exception and use cerr to print out what GetMessage() says.

Step 3: Handle push's error case

Push errors when the stack is full. Write a StackFull class to represent this error at the top of the exceptions.cpp file. This exception should take a string in its constructor and have a GetMessage() accessor for this string. throw this exception with an error message passed to the exception's constructor, instead of printing an error message and doing exit(1). Write the code in main to catch the exception and use cerr to print out what GetMessage() says.

Step 4: Handle pop's error case

Pop errors when the stack is empty. Write a StackEmpty class to represent this error at the top of the exceptions.cpp file. This exception should take a string in its constructor and have a GetMessage() accessor for this string. throw this exception with an error message passed to the exception's constructor, instead of printing an error message and doing exit(1). Write the code in main to catch the exception and use cerr to print out what GetMessage() says.

Step 5: Compile and Run

Compile the program. Run it. If you did everything correctly, the program should run and should say "Completed!"

Extra Credit

(2 Points) Template IntStack so that it can have any data type, not just int. Don't worry that the old name is misleading.

(2 Points) Change your exceptions so that all three of them derive off of a StackException class. The shared code should be in StackException.