Advertisement
avr39ripe

Pv913ExceptionAdv

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