Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //class Except
- //{
- // int val;
- //public:
- // Except(int pVal) : val{ pVal } {}
- // int getCode() const { return val; }
- //};
- class Except : public std::exception
- {
- std::string errMsg;
- int errCode;
- public:
- Except(std::string pMsg, int code) : errMsg{ pMsg }, errCode{ code } {}
- const char* what() const noexcept { return errMsg.c_str(); }
- int getError() const { return errCode; }
- };
- int getVal(int seed)
- {
- if (seed > 0 and seed < 10)
- {
- std::cout << "Boom!\n";
- return rand() % seed;
- }
- else
- {
- throw Except("Bad seed!", 13);
- }
- }
- int printRandom()
- {
- int result{};
- try
- {
- result = getVal(-7);
- }
- catch (const std::exception& ex)
- {
- std::cout << "oops, error...\n";
- throw;
- }
- std::cout << "Random number iiiis... -> " << result << '\n';
- return result;
- }
- class BaseA
- {
- int val;
- public:
- BaseA(int num) : val{ num }
- {
- if (val < 0) throw 13;
- }
- };
- class ChildA : public BaseA
- {
- public:
- ChildA(int num) try : BaseA{ num } { std::cout << "ChildA ctor!\n"; }
- catch (int ex)
- {
- std::cout << "Cant create BaseA!\n";
- }
- };
- int main()
- {
- ChildA ch{ -333 };
- return 0;
- std::cout << "Program starts\n";
- // std::cout << getVal(-1) << '\n';
- int random{};
- try
- {
- //throw 56.87;
- random = printRandom();
- //std::cout << "This will never prints\n";
- }
- catch (int ex)
- {
- std::cout << "We got int exception with value of " << ex << '\n';
- }
- catch (float ex)
- {
- std::cout << "We got float exception with value of " << ex << '\n';
- }
- catch (const Except& ex)
- {
- //std::cout << "We got Except exception with value of " << ex.what() << " with code = " << ex.getError() << '\n';
- if (ex.getError() == 13)
- {
- random = 42;
- }
- }
- catch (const std::exception& ex)
- {
- std::cout << "We got std:exception exception " << ex.what() << '\n';
- }
- catch (...)
- {
- std::cout << "Too bad! :(";
- }
- std::cout << "Random val is -> " << random << '\n';
- std::cout << "Program ends\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement