Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <exception>
- class StorageException : public std::exception
- {
- std::string error;
- public:
- StorageException(std::string errorP) : error{ errorP } {};
- const char* what()const noexcept { return error.c_str(); };
- };
- class FuncException : public StorageException
- {
- public:
- FuncException(std::string errorP): StorageException{ errorP }
- {
- //constructor body
- };
- };
- void funcThree()
- {
- std::cout << "FuncThree Started!\n";
- //throw nullptr;
- //throw "Oops!";
- //throw 'Z';
- //throw -1;
- //throw FuncException{ "All bad :(" };
- //throw std::runtime_error{ "FuncThree All Bad:(" };
- std::cout << "FuncThree Ended!\n";
- }
- void funcTwo()
- {
- std::cout << "FuncTwo Started!\n";
- try
- {
- funcThree();
- }
- catch (char ex)
- {
- std::cout << "FuncTwo catches CHAR exception!\n";
- }
- std::cout << "FuncTwo Ended!\n";
- }
- void funcOne()
- {
- std::cout << "FuncOne Started!\n";
- try
- {
- funcTwo();
- }
- catch (float ex)
- {
- std::cout << "FuncOne catches FLOAT exception!\n";
- }
- catch (bool ex)
- {
- std::cout << "FuncOne catches BOOL exception!\n";
- }
- catch (int ex)
- {
- std::cout << "FuncOne catches INT exception!\n";
- }
- std::cout << "FuncOne Ended!\n";
- }
- class Storage
- {
- int arr[5]{ 1,2,3,4,5 };
- public:
- int& operator[](int idx)
- {
- if (idx >= 0 and idx < 5)
- {
- return arr[idx];
- }
- if (idx < 0)
- {
- throw StorageException{ "Too small index!" };
- }
- else
- {
- throw StorageException{ "Too big index!" };
- }
- };
- };
- int main()
- {
- Storage arr;
- int* ptr{ nullptr };
- try
- {
- ptr = new int[42]{};
- funcThree();
- std::cout << arr[-4] << '\n';
- // use ptr
- delete[] ptr;
- //std::string str;
- //str.resize(LONG_MAX);
- }
- //catch (const FuncException& ex)
- //{
- // std::cout << "FuncException: " << ex.what() << '\n';
- //}
- //catch (const StorageException& ex)
- //{
- // std::cout << "StorageException: " << ex.what() << '\n';
- //}
- catch (const std::runtime_error& e) {
- std::cout << "Runtime error: " << e.what() << '\n';
- }
- catch (const std::bad_alloc& e) {
- std::cout << "Allocation failed: " << e.what() << '\n';
- }
- catch (std::exception& ex)
- {
- std::cout << "STD LIB exception " << ex.what() << '\n';
- }
- catch (int ex)
- {
- if (ex == -1)
- {
- std::cout << "funcThree error!\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement