Vserosbuybuy

Exceptions

Apr 6th, 2021
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <fstream>
  4. #include <math.h>
  5. #include <vector>
  6. #include <string>
  7. #include <sstream>
  8. #include <map>
  9. #include <set>
  10.  
  11. using namespace std;
  12.  
  13. class MyExc {
  14. private:
  15.     string _msg;
  16. public:
  17.     MyExc(const string& msg) : _msg(msg) {};
  18.  
  19.     const string& getMsg() const {
  20.         return _msg;
  21.     }
  22. };
  23.  
  24. void run() {
  25.     int a[100];
  26.     int init_sz = 10;
  27.  
  28.     for (int i = 0; i < init_sz; ++i) {
  29.         a[i] = i * 29;
  30.     }
  31.  
  32.     int n;
  33.     cin >> n;
  34.  
  35.     throw MyExc("Ecx in run");
  36.  
  37.     try {
  38.         if (n < 0) {
  39.             string s = "Error: n < 0\n";
  40.             throw s;
  41.         }
  42.  
  43.         if (n >= init_sz) {
  44.             throw "Error: n >= init_sz\n";
  45.         }
  46.         throw 1;
  47.  
  48.         cout << a[n] << endl;
  49.     }
  50.     catch (const char* e) {
  51.         cerr << "Char* error: " << e << endl;
  52.     }
  53.     catch (const string& e) {
  54.         cout << "String error: " << e << endl;
  55.     }
  56.  
  57.     cout << "End of run" << endl;
  58. }
  59.  
  60. int main() {
  61.     try {
  62.         run();
  63.         cout << "after run\n";
  64.     }
  65.     catch (const MyExc& a) {
  66.         cerr << "Exc in main " << a.getMsg() << "\n";
  67.     }
  68.     cout << "End\n";
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment