Advertisement
avr39ripe

cppExceptionFinale

Sep 15th, 2021
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. //class Except
  4. //{
  5. //  int val;
  6. //public:
  7. //  Except(int pVal) : val{ pVal } {}
  8. //  int getCode() const { return val; }
  9. //};
  10.  
  11.  
  12. class Except : public std::exception
  13. {
  14.     std::string errMsg;
  15.     int errCode;
  16. public:
  17.     Except(std::string pMsg, int code) : errMsg{ pMsg }, errCode{ code } {}
  18.     const char* what() const noexcept { return errMsg.c_str(); }
  19.     int getError() const { return errCode; }
  20. };
  21.  
  22.  
  23. int getVal(int seed)
  24. {
  25.     if (seed > 0 and seed < 10)
  26.     {
  27.         std::cout << "Boom!\n";
  28.         return rand() % seed;
  29.     }
  30.     else
  31.     {
  32.         throw Except("Bad seed!", 13);
  33.     }
  34. }
  35.  
  36.  
  37. int printRandom()
  38. {
  39.     int result{};
  40.     try
  41.     {
  42.         result =  getVal(-7);  
  43.     }
  44.     catch (const std::exception& ex)
  45.     {
  46.         std::cout << "oops, error...\n";
  47.  
  48.         throw;
  49.     }
  50.  
  51.     std::cout << "Random number iiiis... -> " << result << '\n';
  52.     return result;
  53.  
  54. }
  55.  
  56. class BaseA
  57. {
  58.     int val;
  59. public:
  60.     BaseA(int num) : val{ num }
  61.     {
  62.         if (val < 0) throw 13;
  63.     }
  64. };
  65.  
  66.  
  67. class ChildA : public BaseA
  68. {
  69. public:
  70.     ChildA(int num) try : BaseA{ num } { std::cout << "ChildA ctor!\n"; }
  71.     catch (int ex)
  72.     {
  73.         std::cout << "Cant create BaseA!\n";
  74.     }
  75. };
  76.  
  77. int main()
  78. {
  79.     ChildA ch{ -333 };
  80.  
  81.     return 0;
  82.  
  83.     std::cout << "Program starts\n";
  84.  
  85.  
  86. //  std::cout << getVal(-1) << '\n';
  87.  
  88.     int random{};
  89.  
  90.     try
  91.     {
  92.         //throw 56.87;
  93.  
  94.         random = printRandom();
  95.  
  96.         //std::cout << "This will never prints\n";
  97.     }
  98.    
  99.     catch (int ex)
  100.     {
  101.  
  102.         std::cout << "We got int exception with value of " << ex << '\n';
  103.  
  104.     }
  105.  
  106.     catch (float ex)
  107.     {
  108.  
  109.         std::cout << "We got float exception with value of " << ex << '\n';
  110.  
  111.     }
  112.  
  113.  
  114.  
  115.     catch (const Except& ex)
  116.     {
  117.         //std::cout << "We got Except exception with value of " << ex.what() << " with code = " << ex.getError() << '\n';
  118.         if (ex.getError() == 13)
  119.         {
  120.             random = 42;
  121.         }
  122.  
  123.     }
  124.  
  125.     catch (const std::exception& ex)
  126.     {
  127.         std::cout << "We got std:exception exception " << ex.what() << '\n';
  128.     }
  129.  
  130.     catch (...)
  131.     {
  132.         std::cout << "Too bad! :(";
  133.     }
  134.  
  135.     std::cout << "Random val is -> " << random << '\n';
  136.  
  137.  
  138.     std::cout << "Program ends\n";
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement