#include #include #include #include #include #include #include class Error : public std::exception { public: explicit Error(const std::string& what): _what(what) {} explicit Error(const char* what): Error(std::string(what)) {} explicit Error(std::string_view what): Error(std::string(what)) {} const char* what() const noexcept override { return _what.c_str(); } private: std::string _what; }; class ParserException : public Error { public: using Error::Error; }; class ReaderException : public Error { public: using Error::Error; }; int parse(const std::string& str) { if (str.empty()) { throw ParserException("String is empty!"); } if (str.size() > 20) { throw ParserException("Value is to big to fit in integer!"); } return std::stoi(str); } std::string read(const std::vector& vec) { if (vec.empty()) { throw ReaderException("Vector is empty, can't read"); } if (vec.size() > 20) { throw ReaderException("Vector is to big"); return ""; } return std::accumulate(vec.begin(), vec.end(), std::string{}, [](const auto& str, int num){ if (str.empty()) { return std::to_string(num); } return str + ", " + std::to_string(num); }); } int main() { try { std::cout << "Parsed number: " << parse("-123") << '\n'; std::cout << "Read numbers: " << read({1,2,3-1,-2,-3}) << '\n'; std::cout << "Parsed number: " << parse("123456789012345678901234") << '\n'; std::cout << "Read numbers: " << read({}) << '\n'; } catch (const ReaderException& ec) { std::cout << "ReaderException: " << ec.what() << '\n'; } catch (const ParserException& ec) { std::cout << "ParserException: " << ec.what() << '\n'; } }