CKTIS / C++
std::invalid_argument — invalid argument exception
std::invalid_argument from <stdexcept> reports an invalid argument. It carries a diagnostic message available through what() and belongs to the std::logic_error exception hierarchy.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <stdexcept>
// C++23 constructors:
explicit invalid_argument(const std::string& what_arg);
explicit invalid_argument(const char* what_arg);
throw std::invalid_argument("message");std::invalid_argument is an exception type from <stdexcept> intended to report an invalid argument. It publicly derives from std::logic_error and indirectly from std::exception.
Its constructors accept a message as const std::string& or const char*. The what() method exposes text matching the supplied message under C-string comparison. Merely constructing an object does not throw it as an exception: use throw to report it.
In the example, require_name explicitly rejects an empty name. The catch block handles the exception through const std::invalid_argument& and prints the message. Checking the argument and deciding to throw are part of this function, not automatic validation supplied by the exception type.
This entry targets C++23. The supplied source is an evolving standard draft and also shows constexpr constructors; this does not establish their availability as constexpr in C++23. The example does not require that usage. A compiler flag does not attest production runtime support.
Examples
#include <iostream>
#include <stdexcept>
#include <string>
void require_name(const std::string& name) {
if (name.empty()) {
throw std::invalid_argument("name must not be empty");
}
}
int main() {
try {
require_name("");
} catch (const std::invalid_argument& error) {
std::cout << error.what() << '\n';
}
}
Result
name must not be empty
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.