Files
cppdraft_translate/cppdraft/std/exceptions.md
2025-10-25 03:02:53 +03:00

360 lines
13 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[std.exceptions]
# 19 Diagnostics library [[diagnostics]](./#diagnostics)
## 19.2 Exception classes [std.exceptions]
### [19.2.1](#general) General [[std.exceptions.general]](std.exceptions.general)
[1](#general-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L31)
The C++ standard library provides classes to be used to report certain errors ([[res.on.exception.handling]](res.on.exception.handling "16.4.6.14Restrictions on exception handling")) in
C++ programs[.](#general-1.sentence-1)
In the error model reflected in these classes, errors are divided into two
broad categories:[*logic*](#def:logic) errors and[*runtime*](#def:runtime) errors[.](#general-1.sentence-2)
[2](#general-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L41)
The distinguishing characteristic of logic errors is that they are due to errors
in the internal logic of the program[.](#general-2.sentence-1)
In theory, they are preventable[.](#general-2.sentence-2)
[3](#general-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L46)
By contrast, runtime errors are due to events beyond the scope of the program[.](#general-3.sentence-1)
They cannot be easily predicted in advance[.](#general-3.sentence-2)
The header <stdexcept> defines several types of predefined exceptions for reporting errors in a C++ program[.](#general-3.sentence-3)
These exceptions are related by inheritance[.](#general-3.sentence-4)
### [19.2.2](#stdexcept.syn) Header <stdexcept> synopsis [[stdexcept.syn]](stdexcept.syn)
[🔗](#lib:logic_error)
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;}
### [19.2.3](#logic.error) Class logic_error [[logic.error]](logic.error)
[🔗](#lib:logic_error_)
namespace std {class logic_error : public exception {public:constexpr explicit logic_error(const string& what_arg); constexpr explicit logic_error(const char* what_arg); };}
[1](#logic.error-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L91)
The classlogic_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-1.sentence-1)
[🔗](#lib:logic_error,constructor)
`constexpr logic_error(const string& what_arg);
`
[2](#logic.error-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L105)
*Postconditions*: strcmp(what(), what_arg.c_str()) == 0[.](#logic.error-2.sentence-1)
[🔗](#lib:logic_error,constructor_)
`constexpr logic_error(const char* what_arg);
`
[3](#logic.error-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L116)
*Postconditions*: strcmp(what(), what_arg) == 0[.](#logic.error-3.sentence-1)
### [19.2.4](#domain.error) Class domain_error [[domain.error]](domain.error)
[🔗](#lib:domain_error_)
namespace std {class domain_error : public logic_error {public:constexpr explicit domain_error(const string& what_arg); constexpr explicit domain_error(const char* what_arg); };}
[1](#domain.error-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L134)
The classdomain_error defines the type of objects thrown as
exceptions by the implementation to report domain errors[.](#domain.error-1.sentence-1)
[🔗](#lib:domain_error,constructor)
`constexpr domain_error(const string& what_arg);
`
[2](#domain.error-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L146)
*Postconditions*: strcmp(what(), what_arg.c_str()) == 0[.](#domain.error-2.sentence-1)
[🔗](#lib:domain_error,constructor_)
`constexpr domain_error(const char* what_arg);
`
[3](#domain.error-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L157)
*Postconditions*: strcmp(what(), what_arg) == 0[.](#domain.error-3.sentence-1)
### [19.2.5](#invalid.argument) Class invalid_argument [[invalid.argument]](invalid.argument)
[🔗](#lib:invalid_argument_)
namespace std {class invalid_argument : public logic_error {public:constexpr explicit invalid_argument(const string& what_arg); constexpr explicit invalid_argument(const char* what_arg); };}
[1](#invalid.argument-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L175)
The classinvalid_argument defines the type of objects thrown as exceptions to report an invalid argument[.](#invalid.argument-1.sentence-1)
[🔗](#lib:invalid_argument,constructor)
`constexpr invalid_argument(const string& what_arg);
`
[2](#invalid.argument-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L186)
*Postconditions*: strcmp(what(), what_arg.c_str()) == 0[.](#invalid.argument-2.sentence-1)
[🔗](#lib:invalid_argument,constructor_)
`constexpr invalid_argument(const char* what_arg);
`
[3](#invalid.argument-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L197)
*Postconditions*: strcmp(what(), what_arg) == 0[.](#invalid.argument-3.sentence-1)
### [19.2.6](#length.error) Class length_error [[length.error]](length.error)
[🔗](#lib:length_error_)
namespace std {class length_error : public logic_error {public:constexpr explicit length_error(const string& what_arg); constexpr explicit length_error(const char* what_arg); };}
[1](#length.error-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L215)
The classlength_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-1.sentence-1)
[🔗](#lib:length_error,constructor)
`constexpr length_error(const string& what_arg);
`
[2](#length.error-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L228)
*Postconditions*: strcmp(what(), what_arg.c_str()) == 0[.](#length.error-2.sentence-1)
[🔗](#lib:length_error,constructor_)
`constexpr length_error(const char* what_arg);
`
[3](#length.error-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L239)
*Postconditions*: strcmp(what(), what_arg) == 0[.](#length.error-3.sentence-1)
### [19.2.7](#out.of.range) Class out_of_range [[out.of.range]](out.of.range)
[🔗](#lib:out_of_range_)
namespace std {class out_of_range : public logic_error {public:constexpr explicit out_of_range(const string& what_arg); constexpr explicit out_of_range(const char* what_arg); };}
[1](#out.of.range-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L257)
The classout_of_range defines the type of objects thrown as exceptions to report an
argument value not in its expected range[.](#out.of.range-1.sentence-1)
[🔗](#lib:out_of_range,constructor)
`constexpr out_of_range(const string& what_arg);
`
[2](#out.of.range-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L270)
*Postconditions*: strcmp(what(), what_arg.c_str()) == 0[.](#out.of.range-2.sentence-1)
[🔗](#lib:out_of_range,constructor_)
`constexpr out_of_range(const char* what_arg);
`
[3](#out.of.range-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L281)
*Postconditions*: strcmp(what(), what_arg) == 0[.](#out.of.range-3.sentence-1)
### [19.2.8](#runtime.error) Class runtime_error [[runtime.error]](runtime.error)
[🔗](#lib:runtime_error_)
namespace std {class runtime_error : public exception {public:constexpr explicit runtime_error(const string& what_arg); constexpr explicit runtime_error(const char* what_arg); };}
[1](#runtime.error-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L299)
The classruntime_error defines the type of objects thrown as exceptions to report errors presumably detectable only
when the program executes[.](#runtime.error-1.sentence-1)
[🔗](#lib:runtime_error,constructor)
`constexpr runtime_error(const string& what_arg);
`
[2](#runtime.error-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L311)
*Postconditions*: strcmp(what(), what_arg.c_str()) == 0[.](#runtime.error-2.sentence-1)
[🔗](#lib:runtime_error,constructor_)
`constexpr runtime_error(const char* what_arg);
`
[3](#runtime.error-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L322)
*Postconditions*: strcmp(what(), what_arg) == 0[.](#runtime.error-3.sentence-1)
### [19.2.9](#range.error) Class range_error [[range.error]](range.error)
[🔗](#lib:range_error_)
namespace std {class range_error : public runtime_error {public:constexpr explicit range_error(const string& what_arg); constexpr explicit range_error(const char* what_arg); };}
[1](#range.error-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L340)
The classrange_error defines the type of objects thrown as exceptions to report range errors
in internal computations[.](#range.error-1.sentence-1)
[🔗](#lib:range_error,constructor)
`constexpr range_error(const string& what_arg);
`
[2](#range.error-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L352)
*Postconditions*: strcmp(what(), what_arg.c_str()) == 0[.](#range.error-2.sentence-1)
[🔗](#lib:range_error,constructor_)
`constexpr range_error(const char* what_arg);
`
[3](#range.error-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L363)
*Postconditions*: strcmp(what(), what_arg) == 0[.](#range.error-3.sentence-1)
### [19.2.10](#overflow.error) Class overflow_error [[overflow.error]](overflow.error)
[🔗](#lib:overflow_error_)
namespace std {class overflow_error : public runtime_error {public:constexpr explicit overflow_error(const string& what_arg); constexpr explicit overflow_error(const char* what_arg); };}
[1](#overflow.error-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L381)
The classoverflow_error defines the type of objects thrown as exceptions to report an arithmetic overflow error[.](#overflow.error-1.sentence-1)
[🔗](#lib:overflow_error,constructor)
`constexpr overflow_error(const string& what_arg);
`
[2](#overflow.error-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L392)
*Postconditions*: strcmp(what(), what_arg.c_str()) == 0[.](#overflow.error-2.sentence-1)
[🔗](#lib:overflow_error,constructor_)
`constexpr overflow_error(const char* what_arg);
`
[3](#overflow.error-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L403)
*Postconditions*: strcmp(what(), what_arg) == 0[.](#overflow.error-3.sentence-1)
### [19.2.11](#underflow.error) Class underflow_error [[underflow.error]](underflow.error)
[🔗](#lib:underflow_error_)
namespace std {class underflow_error : public runtime_error {public:constexpr explicit underflow_error(const string& what_arg); constexpr explicit underflow_error(const char* what_arg); };}
[1](#underflow.error-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L421)
The classunderflow_error defines the type of objects thrown as exceptions to report an arithmetic underflow error[.](#underflow.error-1.sentence-1)
[🔗](#lib:underflow_error,constructor)
`constexpr underflow_error(const string& what_arg);
`
[2](#underflow.error-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L432)
*Postconditions*: strcmp(what(), what_arg.c_str()) == 0[.](#underflow.error-2.sentence-1)
[🔗](#lib:underflow_error,constructor_)
`constexpr underflow_error(const char* what_arg);
`
[3](#underflow.error-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/diagnostics.tex#L443)
*Postconditions*: strcmp(what(), what_arg) == 0[.](#underflow.error-3.sentence-1)