By contrast, runtime errors are due to events beyond the scope of the program
. They cannot be easily predicted in advance
. The header
<stdexcept>
defines several types of predefined exceptions for reporting errors in a C++ program
. These exceptions are related by inheritance
.
namespace std {
class logic_error : public exception {
public:
explicit logic_error(const string& what_arg);
explicit logic_error(const char* what_arg);
};
}
The class
logic_error
defines the type of objects thrown as
exceptions to report errors presumably detectable before
the program executes, such as violations of logical preconditions or class
invariants
. logic_error(const string& what_arg);
Effects:
Constructs an object of class
logic_error. Postconditions:
strcmp(what(), what_arg.c_str()) == 0. logic_error(const char* what_arg);
Effects:
Constructs an object of class
logic_error. Postconditions:
strcmp(what(), what_arg) == 0.
namespace std {
class domain_error : public logic_error {
public:
explicit domain_error(const string& what_arg);
explicit domain_error(const char* what_arg);
};
}
The class
domain_error
defines the type of objects thrown as
exceptions by the implementation to report domain errors
. domain_error(const string& what_arg);
Effects:
Constructs an object of class
domain_error. Postconditions:
strcmp(what(), what_arg.c_str()) == 0. domain_error(const char* what_arg);
Effects:
Constructs an object of class
domain_error. Postconditions:
strcmp(what(), what_arg) == 0.
namespace std {
class invalid_argument : public logic_error {
public:
explicit invalid_argument(const string& what_arg);
explicit invalid_argument(const char* what_arg);
};
}
The class
invalid_argument
defines the type of objects thrown as exceptions to report an invalid argument
. invalid_argument(const string& what_arg);
Effects:
Constructs an object of class
invalid_argument. Postconditions:
strcmp(what(), what_arg.c_str()) == 0. invalid_argument(const char* what_arg);
Effects:
Constructs an object of class
invalid_argument. Postconditions:
strcmp(what(), what_arg) == 0.
namespace std {
class length_error : public logic_error {
public:
explicit length_error(const string& what_arg);
explicit length_error(const char* what_arg);
};
}
The class
length_error
defines the type of objects thrown as exceptions
to report an attempt to produce
an object whose length exceeds its maximum allowable size
. length_error(const string& what_arg);
Effects:
Constructs an object of class
length_error. Postconditions:
strcmp(what(), what_arg.c_str()) == 0. length_error(const char* what_arg);
Effects:
Constructs an object of class
length_error. Postconditions:
strcmp(what(), what_arg) == 0.
namespace std {
class out_of_range : public logic_error {
public:
explicit out_of_range(const string& what_arg);
explicit out_of_range(const char* what_arg);
};
}
The class
out_of_range
defines the type of objects thrown as exceptions to report an
argument value not in its expected range
. out_of_range(const string& what_arg);
Effects:
Constructs an object of class
out_of_range. Postconditions:
strcmp(what(), what_arg.c_str()) == 0. out_of_range(const char* what_arg);
Effects:
Constructs an object of class
out_of_range. Postconditions:
strcmp(what(), what_arg) == 0.
namespace std {
class runtime_error : public exception {
public:
explicit runtime_error(const string& what_arg);
explicit runtime_error(const char* what_arg);
};
}
The class
runtime_error
defines the type of objects thrown as exceptions to report errors presumably detectable only
when the program executes
. runtime_error(const string& what_arg);
Effects:
Constructs an object of class
runtime_error. Postconditions:
strcmp(what(), what_arg.c_str()) == 0. runtime_error(const char* what_arg);
Effects:
Constructs an object of class
runtime_error. Postconditions:
strcmp(what(), what_arg) == 0.
namespace std {
class range_error : public runtime_error {
public:
explicit range_error(const string& what_arg);
explicit range_error(const char* what_arg);
};
}
The class
range_error
defines the type of objects thrown as exceptions to report range errors
in internal computations
. range_error(const string& what_arg);
Effects:
Constructs an object of class
range_error. Postconditions:
strcmp(what(), what_arg.c_str()) == 0. range_error(const char* what_arg);
Effects:
Constructs an object of class
range_error. Postconditions:
strcmp(what(), what_arg) == 0.
namespace std {
class overflow_error : public runtime_error {
public:
explicit overflow_error(const string& what_arg);
explicit overflow_error(const char* what_arg);
};
}
The class
overflow_error
defines the type of objects thrown as exceptions to report an arithmetic overflow error
. overflow_error(const string& what_arg);
Effects:
Constructs an object of class
overflow_error. Postconditions:
strcmp(what(), what_arg.c_str()) == 0. overflow_error(const char* what_arg);
Effects:
Constructs an object of class
overflow_error. Postconditions:
strcmp(what(), what_arg) == 0.
namespace std {
class underflow_error : public runtime_error {
public:
explicit underflow_error(const string& what_arg);
explicit underflow_error(const char* what_arg);
};
}
The class
underflow_error
defines the type of objects thrown as exceptions to report an arithmetic underflow error
. underflow_error(const string& what_arg);
Effects:
Constructs an object of class
underflow_error. Postconditions:
strcmp(what(), what_arg.c_str()) == 0. underflow_error(const char* what_arg);
Effects:
Constructs an object of class
underflow_error. Postconditions:
strcmp(what(), what_arg) == 0.