invalid_argument exception defines the type of objects thrown as exceptions to report an invalid argument.
It is one of the types of standard exceptions.
Some components of the standard library also throw exceptions of this type to signal invalid arguments.
Example:
class invalid_argument : public logic_error {
public:
explicit invalid_argument (conststring& what_arg);
};
constructor
The string passed as what_arg has the same content as the value returned by member what.
The classinheritsthewhatmemberfunctionfromlogic_error.
Samplecode:// invalid_argument example#include<iostream> // std::cerr#include<stdexcept> // std::invalid_argument#include<bitset> // std::bitset#include<string> // std::stringintmain(void){
try {
// bitset constructor throws an invalid_argument if initialized// with a string containing characters other than 0 and 1std::bitset<5> mybitset (std::string("01234"));
}
catch (conststd::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
}
return0;
}
Comments