I was wondering if this should be considered OK to throw exceptions that are defined in the C ++ standard library, instead of creating my own class. For example, consider the following (dumb) function that takes a single line as an argument:
#include <stdexcept> #include <iostream> #include <string> bool useless_function(const std::string& str) { if (str == "true") return true; else if (str == "false") return false; else throw std::invalid_argument("Expected argument of either true or false"); }
and then, of course, we could do something like:
int main(int argc, const char** argv) { try { const bool check = useless_function("not true"); } catch (std::invalid_argument& error) { std::cerr << error.what() << '\n'; } return 0; }
I read here that the std::stoi family of functions throws a std::invalid_exception when they receive an invalid argument; that when the idea came up above.
source share