Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. /* CS 1415 - Lab 11               *
  2.  * Exception Handling             *
  3.  * Created By: Bryce Jacobson     *
  4.  * 03/28/17                       */
  5.  
  6. #include "JacoBrycExceptions.h"
  7.  
  8. using namespace Jacobson;
  9.  
  10. // Function Prototypes
  11. void Menu();
  12. void OverflowFunction();
  13. void SubscriptFunction();
  14. void MemoryFunction();
  15.  
  16. // The purpose of this program is to test
  17. // different exceptions using classes that we have
  18. // created. We use 3 functions to create our environments
  19. // that we will need to throw our exception.
  20.  
  21. // Program entry point
  22. int main()
  23. {
  24.     Menu();
  25.  
  26.     return 0;
  27. }
  28.  
  29. // This will be the core to our program, it will prompt the user
  30. // and it will also call our functions to test each exception.
  31. // This menu will give the user the chance to try again if they so desire
  32. // to.
  33. void Menu()
  34. {
  35.     int userChoice;
  36.     char userTryAgain;
  37.     bool goAgain;
  38.  
  39.     cout << "Welcome to The Exception Handling Test Program\n"
  40.          << "this program will test three different exceptions.\n"
  41.          << "These exceptions have been created in a specialized\n"
  42.          << "environment in order to prevent catastrophic results\n"
  43.          << "we recommend that you do not try this at home." << endl;
  44.     do
  45.     {
  46.         cout << "\nPlease select an Exception Handler to test." << endl;
  47.         cout << "1) Overflow Exception\n"
  48.              << "2) Subscript Exception\n"
  49.              << "3) Memory Exception\n" << endl;
  50.         cin >> userChoice;
  51.         try
  52.         {
  53.             switch (userChoice)
  54.             {
  55.             case 1:
  56.                 cout << "\nInteger Overflow\n" << endl;
  57.                 OverflowFunction();
  58.                 break;
  59.  
  60.             case 2:
  61.                 cout << "\nSubscript Out-of-Bounds\n" << endl;
  62.                 SubscriptFunction();
  63.                 break;
  64.  
  65.             case 3:
  66.                 cout << "\nMemory Allocation Failure\n" << endl;
  67.                 MemoryFunction();
  68.                 break;
  69.             }
  70.         }
  71.         catch (const OverflowException& exception)
  72.         {
  73.             cout << "\nException Thrown: " << exception.what();
  74.         }
  75.         catch (const SubscriptException& exception)
  76.         {
  77.             cout << "\nException Thrown: " << exception.what();
  78.         }
  79.         catch (const MemoryException& exception)
  80.         {
  81.             cout << "\nException Thrown: " << exception.what() << endl;
  82.         }
  83.  
  84.         cout << "\nGo again? (y/n)" << endl;
  85.         cin >> userTryAgain;
  86.        
  87.         if (userTryAgain == 'y')
  88.             goAgain = true;
  89.         else
  90.             goAgain = false;
  91.  
  92.     } while (goAgain);
  93. }
  94.  
  95. // OverflowFunction will take no parameters, it will also
  96. // return nothing. This function will use a for loop to
  97. // increase the sum until it overflows. Once it reaches that
  98. // negative value it will throw our exception.
  99. void OverflowFunction()
  100. {
  101.     int sum = INT_MAX - 5;
  102.  
  103.     for (int i = 0; i < 10; i++)
  104.     {
  105.         cout << "Sum: " << sum << endl;
  106.         sum += 1;
  107.  
  108.         if (sum < 0)
  109.             throw(OverflowException("arithmetic overflow\n"));
  110.     }
  111. }
  112.  
  113. // SubscriptFunction will take no parameters, it will also
  114. // return nothing. This function will use a for loop to
  115. // increase the index of an array infinitely. Once we hit
  116. // outside the bounds of that array our exception will
  117. // be thrown.
  118. void SubscriptFunction()
  119. {
  120.     const int arrSize = 5;
  121.     int testArr[arrSize] = {};
  122.  
  123.     cout << "Array Size: " << arrSize << endl;
  124.     for (int i = 0; ; i++)
  125.     {
  126.         cout << "Array Index: " << i << endl;
  127.  
  128.         if (i >= arrSize - 1)
  129.             throw(SubscriptException("subscript out-of-bounds\n"));
  130.     }
  131. }
  132.  
  133. // MemoryFunction will take no parameters, it will also
  134. // return nothing. This function will use a while loop to
  135. // create a dynamic array, free the memory and multiply our size by 10.
  136. // Once the new operator fails we will skip the loop and throw the
  137. // bad_allloc exception. From the bad_alloc catch statement we will
  138. // then throw our MemoryException.
  139. void MemoryFunction()
  140. {
  141.     try
  142.     {
  143.         int size = 1;
  144.         while (int* arr = new int[size])
  145.         {
  146.             cout << "Creating array of size: " << size << endl;
  147.             delete[] arr;
  148.             size *= 10;
  149.         }
  150.     }
  151.  
  152.     catch(bad_alloc& ba)
  153.     {
  154.         throw(MemoryException(ba.what()));
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement