Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. // Function Prototypes
  2. void Menu();
  3. void OverflowFunction();
  4. void SubscriptFunction();
  5. void MemoryFunction();
  6.  
  7. // The purpose of this program is to test
  8. // different exceptions using classes that we have
  9. // created. We use 3 functions to create our environments
  10. // that we will need to throw our exception.
  11.  
  12. // Program entry point
  13. int main()
  14. {
  15.     Menu();
  16.  
  17.     return 0;
  18. }
  19.  
  20. // This will be the core to our program, it will prompt the user
  21. // and it will also call our functions to test each exception.
  22. // This menu will give the user the chance to try again if they so desire
  23. // to.
  24. void Menu()
  25. {
  26.     int userChoice;
  27.     char userTryAgain;
  28.     bool goAgain;
  29.  
  30.     cout << "Welcome to The Exception Handling Test Program\n"
  31.          << "this program will test three different exceptions.\n"
  32.          << "These exceptions have been created in a specialized\n"
  33.          << "environment in order to prevent catastrophic results\n"
  34.          << "we recommend that you do not try this at home." << endl;
  35.     do
  36.     {
  37.         cout << "\nPlease select an Exception Handler to test." << endl;
  38.         cout << "1) Overflow Exception\n"
  39.              << "2) Subscript Exception\n"
  40.              << "3) Memory Exception\n" << endl;
  41.         cin >> userChoice;
  42.         try
  43.         {
  44.             switch (userChoice)
  45.             {
  46.             case 1:
  47.                 cout << "\nInteger Overflow\n" << endl;
  48.                 OverflowFunction();
  49.                 break;
  50.  
  51.             case 2:
  52.                 cout << "\nSubscript Out-of-Bounds\n" << endl;
  53.                 SubscriptFunction();
  54.                 break;
  55.  
  56.             case 3:
  57.                 cout << "\nMemory Allocation Failure\n" << endl;
  58.                 MemoryFunction();
  59.                 break;
  60.             }
  61.         }
  62.         catch (const OverflowException& exception)
  63.         {
  64.             cout << "\nException Thrown: " << exception.what();
  65.         }
  66.         catch (const SubscriptException& exception)
  67.         {
  68.             cout << "\nException Thrown: " << exception.what();
  69.         }
  70.         catch (const MemoryException& exception)
  71.         {
  72.             cout << "\nException Thrown: " << exception.what() << endl;
  73.         }
  74.  
  75.         cout << "\nGo again? (y/n)" << endl;
  76.         cin >> userTryAgain;
  77.        
  78.         if (userTryAgain == 'y')
  79.             goAgain = true;
  80.         else
  81.             goAgain = false;
  82.  
  83.     } while (goAgain);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement