Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* CS 1415 - Lab 11 *
- * Exception Handling *
- * Created By: Bryce Jacobson *
- * 03/28/17 */
- #include "JacoBrycExceptions.h"
- using namespace Jacobson;
- // Function Prototypes
- void Menu();
- void OverflowFunction();
- void SubscriptFunction();
- void MemoryFunction();
- // The purpose of this program is to test
- // different exceptions using classes that we have
- // created. We use 3 functions to create our environments
- // that we will need to throw our exception.
- // Program entry point
- int main()
- {
- Menu();
- return 0;
- }
- // This will be the core to our program, it will prompt the user
- // and it will also call our functions to test each exception.
- // This menu will give the user the chance to try again if they so desire
- // to.
- void Menu()
- {
- int userChoice;
- char userTryAgain;
- bool goAgain;
- cout << "Welcome to The Exception Handling Test Program\n"
- << "this program will test three different exceptions.\n"
- << "These exceptions have been created in a specialized\n"
- << "environment in order to prevent catastrophic results\n"
- << "we recommend that you do not try this at home." << endl;
- do
- {
- cout << "\nPlease select an Exception Handler to test." << endl;
- cout << "1) Overflow Exception\n"
- << "2) Subscript Exception\n"
- << "3) Memory Exception\n" << endl;
- cin >> userChoice;
- try
- {
- switch (userChoice)
- {
- case 1:
- cout << "\nInteger Overflow\n" << endl;
- OverflowFunction();
- break;
- case 2:
- cout << "\nSubscript Out-of-Bounds\n" << endl;
- SubscriptFunction();
- break;
- case 3:
- cout << "\nMemory Allocation Failure\n" << endl;
- MemoryFunction();
- break;
- }
- }
- catch (const OverflowException& exception)
- {
- cout << "\nException Thrown: " << exception.what();
- }
- catch (const SubscriptException& exception)
- {
- cout << "\nException Thrown: " << exception.what();
- }
- catch (const MemoryException& exception)
- {
- cout << "\nException Thrown: " << exception.what() << endl;
- }
- cout << "\nGo again? (y/n)" << endl;
- cin >> userTryAgain;
- if (userTryAgain == 'y')
- goAgain = true;
- else
- goAgain = false;
- } while (goAgain);
- }
- // OverflowFunction will take no parameters, it will also
- // return nothing. This function will use a for loop to
- // increase the sum until it overflows. Once it reaches that
- // negative value it will throw our exception.
- void OverflowFunction()
- {
- int sum = INT_MAX - 5;
- for (int i = 0; i < 10; i++)
- {
- cout << "Sum: " << sum << endl;
- sum += 1;
- if (sum < 0)
- throw(OverflowException("arithmetic overflow\n"));
- }
- }
- // SubscriptFunction will take no parameters, it will also
- // return nothing. This function will use a for loop to
- // increase the index of an array infinitely. Once we hit
- // outside the bounds of that array our exception will
- // be thrown.
- void SubscriptFunction()
- {
- const int arrSize = 5;
- int testArr[arrSize] = {};
- cout << "Array Size: " << arrSize << endl;
- for (int i = 0; ; i++)
- {
- cout << "Array Index: " << i << endl;
- if (i >= arrSize - 1)
- throw(SubscriptException("subscript out-of-bounds\n"));
- }
- }
- // MemoryFunction will take no parameters, it will also
- // return nothing. This function will use a while loop to
- // create a dynamic array, free the memory and multiply our size by 10.
- // Once the new operator fails we will skip the loop and throw the
- // bad_allloc exception. From the bad_alloc catch statement we will
- // then throw our MemoryException.
- void MemoryFunction()
- {
- try
- {
- int size = 1;
- while (int* arr = new int[size])
- {
- cout << "Creating array of size: " << size << endl;
- delete[] arr;
- size *= 10;
- }
- }
- catch(bad_alloc& ba)
- {
- throw(MemoryException(ba.what()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement