This chapter describes objects and facilities used to report error conditions.
This chapter contains objects for error testing and reporting.
The library provides for exception classes for use with logic errors and runtime errors. Logic errors in theory can be predicted in advance while runtime errors can not. The header <stdexcept> predefines several types of exceptions for C++ error reporting.
namespace std {
class logic_error;
class domain_error;
class invalid_argument;
class length_error;
class out_of_range;
class runtime_error;
class range_error;
class overflow_error;
class underflow_error;
}
The logic_error class is derived from the "18.6.1 Class Exception," and is used for exceptions that are detectable before program
execution.
namespace std {
class logic_error : public exception {
public:
explicit logic_error(const string& what_arg);
};
}
Constructs an object of class logic_error. Initializes exception::what to the what_arg argument.
Prototype:
logic_error(const string& what_arg);
A derived class of logic error the domain_error object is used for exceptions of domain errors.
namespace std {
class domain_error : public logic_error {
public:
explicit domain_error(const string& what_arg);
};
}
Constructs an object of domain_error. Initializes exception::what to the what_arg argument
Prototype:
domain_error(const string& what_arg);
A derived class of logic_error the invalid_argument is used for exceptions of invalid arguments.
namespace std {
class invalid_argument : public logic_error {
public:
explicit invalid_argument(const string& what_arg);
};
}
Constructs an object of class invalid_argument. Initializes exception::what to the what_arg argument
Prototype:
invalid_argument(const string& what_arg);
A derived class of logic_error the length_error is use to report exceptions when an object exceeds allowed sizes.
namespace std {
class length_error : public logic_error {
public:
explicit length_error(const string& what_arg);
};
}
Constructs an object of class length_error. Initializes exception::what to the what_arg argument
Prototype:
length_error(const string& what_arg);
A derived class of logic_error an object of out_of_range is used for exceptions for out of range
errors.
namespace std {
class out_of_range : public logic_error {
public:
explicit out_of_range(const string& what_arg);
};
}
Constructs an object of the class out_of_range. Initializes exception::what to the what_arg argument
Prototype:
out_of_range(const string& what_arg);
Derived from the "18.6.1 Class Exception," the runtime_error object is used to report errors detectable only during runtime.
namespace std {
class runtime_error : public exception {
public:
explicit runtime_error(const string& what_arg);
};
}
Constructs an object of the class runtime_error. Initializes exception::what to the what_arg argument
Prototype:
runtime_error(const string& what_arg);
Derived form the runtime_error class, an object of range_error is used for exceptions due to runtime out of range errors.
namespace std {
class range_error : public runtime_error {
public:
explicit range_error(const string& what_arg);
};
}
Constructs an object of the class range_error. Initializes exception::what to the what_arg argument
Prototype:
range_error(const string& what_arg);
The overflow_error object is derived from the class runtime_error and is used to report arithmetical overflow errors.
namespace std {
class overflow_error : public runtime_error {
public:
explicit overflow_error(const string& what_arg);
};
}
Constructs an object of the class overflow_error. Initializes exception::what to the what_arg argument
Prototype:
overflow_error(const string& what_arg);
The class underflow_error is derived from the class runtime_error and is used to report the arithmetical underflow error.
namespace std {
class underflow_error : public runtime_error {
public:
explicit underflow_error(const string& what_arg);
};
}
Constructs an object of the class underflow_error. Initializes exception::what to the what_arg argument
Prototype:
underflow_error(const string& what_arg);
The header <cassert> provides for the assert macro and is used
the same as the standard C header assert.h
The header <cerrno> provides macros: EDOM ERANGE and errno to
be used for domain and range errors reported by using the errno
facility. The <cerrno> header is used the same as standard C header
errno.h