Skip to content

CS 1415 - Lab #12

Objectives:

Program with exception handling

Preparation:

Review Section 16.1 from Starting Out with C++ by Gaddis.

Programming:
  1. Name your files LlllFfffexceptions.h and LlllFfffLab12.cpp where Llll is the first four letters of your last name and Ffff is the first four letters of your first name.
  2. Put all exception classes in one header file.
  3. Define and implement an exception class OverflowException that is derived from the class exception. The class exception is designed to be a base class for other exception classes.
  4. Write a function that generates and throws an arithmetic overflow exception (OverflowException) by repeatedly adding 1 to a sum until the value exceeds the capability of the computer. Use a for loop and begin with an initial sum that is near the maximum value the computer can handle. Repeatedly add 1 to the sum. When the sum no longer increases, the exception should be thrown. The function should output informative messages so that progress can be watched.
  5. Define and implement a class SubscriptException.
  6. Write a function the generates and throws a subscript out-of-bounds exception (SubscriptException) by attempting to step off the end of the array. Use a for loop that begins at index zero and goes "forever". When the index is out-of-bounds, the exception should be thrown. Be sure to actually do something with the array that would make the out-of-bounds exception real. The function should output informative messages so that progress can be watched.
  7. Define and implement a class MemoryException.
  8. Write a function that generates and throws a memory allocation exception (MemoryException) by exhausting the memory of the computer. The function should loop on a request to create dynamic storage via the new operator. Start by allocating an array of size 1. If it succeeds, deallocate the array, multiply the size by 10 and repeat. When the new operator throws a bad_alloc exception, your function should detect that and then throw a MemoryException. The function should print an informative message each time through the loop so that progress can be watched.
  9. Write a driver program that demonstrates the exception handling by generating and handling the arithmetic overflow exception, the array subscript out-of-bounds exception, and the insufficient memory exception. The driver should use a menu that lets the user pick which exception to generate and handle. The program should allow the user to repeat the process. Use one try block with three catch blocks in your driver program.
  10. Verify your program works correctly.
  11. Submit your source code file to ude.wons@nosneros.htrag. Due at 8:00 am on 24 March.