avr39ripe

PV913STDExceptExample

Jul 29th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <exception>
  3.  
  4. class StorageException
  5. {
  6.     std::string error;
  7. public:
  8.     StorageException(std::string errorP) : error{ errorP } {};
  9.  
  10.     const char* getError()const { return error.c_str(); };
  11. };
  12.  
  13. class FuncException : public StorageException
  14. {
  15. public:
  16.     FuncException(std::string errorP) : StorageException{ errorP } {};
  17. };
  18.  
  19.  
  20. void funcThree()
  21. {
  22.     std::cout << "FuncThree Started!\n";
  23.  
  24.     //throw nullptr;
  25.     //throw "Oops!";
  26.     //throw 'Z';
  27.     //throw -1;
  28.     //throw FuncException{ "All bad :(" };
  29.     throw std::runtime_error{ "FuncThree All Bad:(" };
  30.  
  31.     std::cout << "FuncThree Ended!\n";
  32. }
  33.  
  34. void funcTwo()
  35. {
  36.     std::cout << "FuncTwo Started!\n";
  37.    
  38.     try
  39.     {
  40.         funcThree();
  41.     }
  42.    
  43.     catch (char ex)
  44.     {
  45.         std::cout << "FuncTwo catches CHAR exception!\n";
  46.     }
  47.  
  48.     std::cout << "FuncTwo Ended!\n";
  49. }
  50.  
  51.  
  52. void funcOne()
  53. {
  54.     std::cout << "FuncOne Started!\n";
  55.     try
  56.     {
  57.         funcTwo();
  58.     }
  59.    
  60.     catch (float ex)
  61.     {
  62.         std::cout << "FuncOne catches FLOAT exception!\n";
  63.     }
  64.  
  65.     catch (bool ex)
  66.     {
  67.         std::cout << "FuncOne catches BOOL exception!\n";
  68.     }
  69.  
  70.     catch (int ex)
  71.     {
  72.         std::cout << "FuncOne catches INT exception!\n";
  73.     }
  74.  
  75.     std::cout << "FuncOne Ended!\n";
  76. }
  77.  
  78.  
  79.  
  80. class Storage
  81. {
  82.     int arr[5]{1,2,3,4,5};
  83. public:
  84.     int& operator[](int idx)
  85.     {
  86.         if (idx >= 0 and idx < 5)
  87.         {
  88.             return arr[idx];
  89.         }
  90.  
  91.         if (idx < 0)
  92.         {
  93.             throw StorageException{ "Too small index!" };
  94.         }
  95.         else
  96.         {
  97.             throw StorageException{ "Too big index!" };
  98.         }
  99.        
  100. };
  101. };
  102.  
  103. int main()
  104. {
  105.     Storage arr;
  106.     try
  107.     {
  108.         funcThree();
  109.         //std::cout << arr[-4] << '\n';
  110.         //std::string str;
  111.         //str.resize(LONG_MAX);
  112.     }
  113.  
  114.     catch (const FuncException& ex)
  115.     {
  116.         std::cout << "FuncException: " << ex.getError() << '\n';
  117.     }
  118.  
  119.     catch (const StorageException& ex)
  120.     {
  121.         std::cout << "StorageException: " <<  ex.getError() << '\n';
  122.     }
  123.  
  124.     catch (const std::runtime_error& e) {
  125.         std::cout << "Runtime error: " << e.what() << '\n';
  126.     }
  127.  
  128.     catch (const std::bad_alloc& e) {
  129.         std::cout << "Allocation failed: " << e.what() << '\n';
  130.     }
  131.  
  132.     catch (std::exception& ex)
  133.     {
  134.         std::cout << "STD LIB exception " << ex.what() << '\n';
  135.     }
  136.  
  137.     catch (int ex)
  138.     {
  139.         if (ex == -1)
  140.         {
  141.             std::cout << "funcThree error!\n";
  142.         }
  143.     }
  144.    
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment