Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error"

From cppreference.com
< cpp
(now I think errno macros should be an actual page, not a "macro const")
(~ -> diagnostics library + add debugging)
 
(38 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{title|Error handling}}
+
{{title|Diagnostics library}}
{{cpp/error/sidebar}}
+
{{cpp/error/navbar}}
  
 
===Exception handling===
 
===Exception handling===
The header {{tt|<exception>}} provide several classes and functions related to exception handling in C++ programs.
+
The header {{header|exception}} provides several classes and functions related to exception handling in C++ programs.
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | exception}}
+
{{dsc header|exception}}
{{dcl list class | cpp/error/exception/exception | base class for exceptions thrown by the standard library components}}
+
{{dsc inc|cpp/error/dsc exception}}
{{dcl list h2 | Capture and storage of exception objects}}
+
{{dsc h2|Capture and storage of exception objects}}
{{dcl list fun | cpp/error/exception/uncaught_exception | checks if exception handling is currently in progress| notes={{mark c++11}}}}
+
{{dsc inc|cpp/error/dsc uncaught_exception}}
{{dcl list typedef | cpp/error/exception/exception_ptr | shared pointer type for handling exception objects | notes={{mark c++11}}}}
+
{{dsc inc|cpp/error/dsc exception_ptr}}
{{dcl list tfun | cpp/error/exception/make_exception_ptr | creates an {{cpp|std::exception_ptr}} from an exception object| notes={{mark c++11}}}}
+
{{dsc inc|cpp/error/dsc make_exception_ptr}}
{{dcl list fun | cpp/error/exception/current_exception | captures the current exception in a {{cpp|std::exception_ptr}}| notes={{mark c++11}}}}
+
{{dsc inc|cpp/error/dsc current_exception}}
{{dcl list fun | cpp/error/exception/rethrow_exception | throws the exception from an {{cpp|std::exception_ptr}}| notes={{mark c++11}}}}
+
{{dsc inc|cpp/error/dsc rethrow_exception}}
{{dcl list class | cpp/error/exception/nested_exception | a mixin type to capture and store current exceptions| notes={{mark c++11}}}}
+
{{dsc inc|cpp/error/dsc nested_exception}}
{{dcl list tfun | cpp/error/exception/throw_with_nested | throws its argument with {{cpp|std::nested_exception}} mixed in| notes={{mark c++11}}}}
+
{{dsc inc|cpp/error/dsc throw_with_nested}}
{{dcl list tfun | cpp/error/exception/rethrow_if_nested | throws the exception from a {{cpp|std::nested_exception}}| notes={{mark c++11}}}}
+
{{dsc inc|cpp/error/dsc rethrow_if_nested}}
{{dcl list h2 | Handling of failures in exception handling}}
+
{{dsc h2|Handling of failures in exception handling}}
{{dcl list fun | cpp/error/exception/terminate | function called when exception handling fails}}
+
{{dsc header|exception}}
{{dcl list typedef | cpp/error/exception/terminate_handler | the type of the function called by {{cpp|std::terminate}}}}
+
{{dsc inc|cpp/error/dsc terminate}}
{{dcl list fun | cpp/error/exception/get_terminate | obtains the current terminate_handler}}
+
{{dsc inc|cpp/error/dsc terminate_handler}}
{{dcl list fun | cpp/error/exception/set_terminate | changes the function to be called by {{cpp|std::terminate}} }}
+
{{dsc inc|cpp/error/dsc get_terminate}}
{{dcl list h2 | Handling of exception specification violations}}
+
{{dsc inc|cpp/error/dsc set_terminate}}
{{dcl list fun | cpp/error/exception/unexpected | notes={{mark deprecated}} | function called when dynamic exception specification is violated}}
+
{{dsc inc|cpp/error/dsc bad_exception}}
{{dcl list class | cpp/error/exception/bad_exception | notes={{mark deprecated}} | exception thrown when dynamic exception specification is violated, if possible}}
+
{{dsc h2|Handling of exception specification violations {{mark until c++17|removed=yes}}}}
{{dcl list typedef | cpp/error/exception/unexpected_handler | notes={{mark deprecated}} |  the type of the function called by {{cpp|std::unexpected}} }}
+
{{dsc inc|cpp/error/dsc unexpected}}
{{dcl list fun | cpp/error/exception/get_unexpected | notes={{mark deprecated}} | obtains the current unexpected_handler}}
+
{{dsc inc|cpp/error/dsc unexpected_handler}}
{{dcl list fun | cpp/error/exception/set_unexpected | notes={{mark deprecated}} | changes the function to be called by {{cpp|std::unexpected}}}}
+
{{dsc inc|cpp/error/dsc get_unexpected}}
{{dcl list end}}
+
{{dsc inc|cpp/error/dsc set_unexpected}}
 +
{{dsc end}}
  
====Exception categories====
+
===Exception categories===
 +
Several convenience classes are predefined in the header {{tt|<stdexcept>}} to report particular error conditions. These classes can be divided into two categories: ''logic'' errors and ''runtime'' errors. Logic errors are a consequence of faulty logic within the program and may be preventable. Runtime errors are due to events beyond the scope of the program and cannot easily be predicted.
  
Several convenience classes are predefined in the header {{tt|<stdexcept>}} to report particular error conditions. These classes can be divided into two categories: ''logic'' errors and ''runtime'' errors. Logic errors are a consequence of faulty logic within the program and may be preventable. Runtime errors are due to events beyond the scope of the program and can not be easily predicted.
+
{{dsc begin}}
 +
{{dsc header|stdexcept}}
 +
{{dsc inc|cpp/error/inc logic_error}}
 +
{{dsc inc|cpp/error/inc invalid_argument}}
 +
{{dsc inc|cpp/error/inc domain_error}}
 +
{{dsc inc|cpp/error/inc length_error}}
 +
{{dsc inc|cpp/error/inc out_of_range}}
 +
{{dsc inc|cpp/error/inc runtime_error}}
 +
{{dsc inc|cpp/error/inc range_error}}
 +
{{dsc inc|cpp/error/inc overflow_error}}
 +
{{dsc inc|cpp/error/inc underflow_error}}
 +
{{dsc tclass|cpp/error/tx_exception|exception class to cancel atomic transactions|notes={{mark since tm ts}}}}
 +
{{dsc end}}
  
{{dcl list begin}}
+
===Error numbers===
{{dcl list header | stdexcept}}
+
{{dsc begin}}
{{dcl list class | cpp/error/exception/logic_error | exception class to indicate violations of logical preconditions or class invariants}}
+
{{dsc header|cerrno}}
{{dcl list class | cpp/error/exception/invalid_argument | exception class to report invalid arguments}}
+
{{dsc inc|cpp/error/dsc errno}}
{{dcl list class | cpp/error/exception/domain_error | exception class to report domain errors}}
+
{{dsc inc|cpp/error/dsc errno_macros}}
{{dcl list class | cpp/error/exception/length_error | exception class to report attempts to exceed maximum allowed size}}
+
{{dsc end}}
{{dcl list class | cpp/error/exception/out_of_range | exception class to report arguments outside of expected range}}
+
{{dcl list class | cpp/error/exception/runtime_error | exception class to indicate conditions only detectable at run time}}
+
{{dcl list class | cpp/error/exception/range_error | exception class to report range errors in internal computations}}
+
{{dcl list class | cpp/error/exception/overflow_error | exception class to report arithmetic overflows}}
+
{{dcl list class | cpp/error/exception/underflow_error | exception class to report arithmetic underflows}}
+
{{dcl list end}}
+
  
===[[cpp/error/errno_macros|Error numbers]]===
+
===System error===
 +
The header {{tt|<system_error>}} defines types and functions used to report error conditions originating from the operating system, streams I/O, {{lc|std::future}}, or other low-level APIs.
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | cerrno}}
+
{{dsc header|system_error}}
{{dcl list macro const | cpp/error/errno | macro which expands to POSIX-compatible thread-local error number variable}}
+
{{dsc inc|cpp/error/dsc error_category}}
{{dcl list end}}
+
{{dsc inc|cpp/error/dsc generic_category}}
 +
{{dsc inc|cpp/error/dsc system_category}}
 +
{{dsc inc|cpp/error/dsc error_condition}}
 +
{{dsc inc|cpp/error/dsc errc}}
 +
{{dsc inc|cpp/error/dsc error_code}}
 +
{{dsc inc|cpp/error/dsc system_error}}
 +
{{dsc end}}
  
 
===Assertions===
 
===Assertions===
 
 
Assertions help to implement checking of preconditions in programs.
 
Assertions help to implement checking of preconditions in programs.
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list keyword | cpp/keywords/static_assert| notes={{mark c++11}}}}
+
{{dsc header|cassert}}
{{dcl list header | cassert}}
+
{{dsc inc|cpp/error/dsc assert}}
{{dcl list macro fun | cpp/error/assert | aborts the program if the user-specified condition is not {{cpp|true}}. May be disabled for release builds}}
+
{{dsc end}}
{{dcl list end}}
+
 
+
===System error===
+
 
+
The header {{tt|<system_error>}} defines types and functions used to report error conditions originating from the operating system, streams I/O, {{cpp|std::future}}, or other low-level APIs.
+
  
{{dcl list begin}}
+
===[[cpp/utility/basic_stacktrace|Stacktrace]]===
{{dcl list header | system_error}}
+
{{dsc begin}}
{{dcl list class  | cpp/error/system_error/error_category | base class for error categories | notes={{mark c++11}}}}
+
{{dsc header|stacktrace}}
{{dcl list class  | cpp/error/system_error/generic_category | error category indicating generic error| notes={{mark c++11}}}}
+
{{dsc inc|cpp/utility/dsc stacktrace_entry}}
{{dcl list class  | cpp/error/system_error/system_category | error category indicating the operating system as the source of error| notes={{mark c++11}}}}
+
{{dsc inc|cpp/utility/dsc basic_stacktrace}}
 +
{{dsc end}}
  
{{dcl list class  | cpp/error/system_error/error_condition | identifies an error condition| notes={{mark c++11}}}}
+
===Debugging support===
{{dcl list fun  | cpp/error/system_error/make_error_condition | creates an error condition object of specified error_category| notes={{mark c++11}}}}
+
{{dsc begin}}
{{dcl list tclass  | cpp/error/system_error/is_error_condition_enum | identifies an enumeration as an {{cpp|std::error_condition}}| notes={{mark c++11}}}}
+
{{dsc header|debugging}}
{{dcl list class  | cpp/error/system_error/err_c | the {{cpp|std::error_condition}} enumeration listing all standard {{tt|<cerrno>}} macro constants| notes={{mark c++11}}}}
+
{{dsc inc|cpp/utility/dsc breakpoint}}
 +
{{dsc inc|cpp/utility/dsc breakpoint_if_debugging}}
 +
{{dsc inc|cpp/utility/dsc is_debugger_present}}
 +
{{dsc end}}
  
{{dcl list class  | cpp/error/system_error/error_code | holds error code values| notes={{mark c++11}}}}
+
===See also===
{{dcl list fun  | cpp/error/system_error/make_error_code | creates an error code from an error_category| notes={{mark c++11}}}}
+
{{dsc begin}}
{{dcl list tclass  | cpp/error/system_error/is_error_code_enum | identifies a class as an error_code enumeration| notes={{mark c++11}}}}
+
{{dsc inc|cpp/language/dsc static_assert}}
 +
{{dsc see c|c/error|Error handling|nomono=true}}
 +
{{dsc end}}
  
{{dcl list class  | cpp/error/system_error/system_error | exception class used to report conditions that have an error_code| notes={{mark c++11}}}}
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
{{dcl list end}}
+

Latest revision as of 20:54, 30 October 2024

 
 
 

Contents

[edit] Exception handling

The header <exception> provides several classes and functions related to exception handling in C++ programs.

Defined in header <exception>
base class for exceptions thrown by the standard library components
(class) [edit]
Capture and storage of exception objects
(removed in C++20*)(C++17)
checks if exception handling is currently in progress
(function) [edit]
shared pointer type for handling exception objects
(typedef) [edit]
creates an std::exception_ptr from an exception object
(function template) [edit]
captures the current exception in a std::exception_ptr
(function) [edit]
throws the exception from an std::exception_ptr
(function) [edit]
a mixin type to capture and store current exceptions
(class) [edit]
throws its argument with std::nested_exception mixed in
(function template) [edit]
throws the exception from a std::nested_exception
(function template) [edit]
Handling of failures in exception handling
Defined in header <exception>
function called when exception handling fails
(function) [edit]
the type of the function called by std::terminate
(typedef) [edit]
obtains the current terminate_handler
(function) [edit]
changes the function to be called by std::terminate
(function) [edit]
exception thrown when std::current_exception fails to copy the exception object
(class) [edit]
Handling of exception specification violations (removed in C++17)
(deprecated in C++11)(removed in C++17)
function called when dynamic exception specification is violated
(function) [edit]
(deprecated in C++11)(removed in C++17)
the type of the function called by std::unexpected
(typedef) [edit]
(deprecated in C++11)(removed in C++17)
obtains the current unexpected_handler
(function) [edit]
(deprecated in C++11)(removed in C++17)
changes the function to be called by std::unexpected
(function) [edit]

[edit] Exception categories

Several convenience classes are predefined in the header <stdexcept> to report particular error conditions. These classes can be divided into two categories: logic errors and runtime errors. Logic errors are a consequence of faulty logic within the program and may be preventable. Runtime errors are due to events beyond the scope of the program and cannot easily be predicted.

Defined in header <stdexcept>
exception class to indicate violations of logical preconditions or class invariants
(class) [edit]
exception class to report invalid arguments
(class) [edit]
exception class to report domain errors
(class) [edit]
exception class to report attempts to exceed maximum allowed size
(class) [edit]
exception class to report arguments outside of expected range
(class) [edit]
exception class to indicate conditions only detectable at run time
(class) [edit]
exception class to report range errors in internal computations
(class) [edit]
exception class to report arithmetic overflows
(class) [edit]
exception class to report arithmetic underflows
(class) [edit]
exception class to cancel atomic transactions
(class template)

[edit] Error numbers

Defined in header <cerrno>
macro which expands to POSIX-compatible thread-local error number variable
(macro variable)[edit]
macros for standard POSIX-compatible error conditions
(macro constant) [edit]

[edit] System error

The header <system_error> defines types and functions used to report error conditions originating from the operating system, streams I/O, std::future, or other low-level APIs.

Defined in header <system_error>
base class for error categories
(class) [edit]
identifies the generic error category
(function) [edit]
identifies the operating system error category
(function) [edit]
holds a portable error code
(class) [edit]
(C++11)
the std::error_condition enumeration listing all standard <cerrno> macro constants
(class) [edit]
holds a platform-dependent error code
(class) [edit]
exception class used to report conditions that have an error_code
(class) [edit]

[edit] Assertions

Assertions help to implement checking of preconditions in programs.

Defined in header <cassert>
aborts the program if the user-specified condition is not true. May be disabled for release builds.
(function macro) [edit]

[edit] Stacktrace

Defined in header <stacktrace>
representation of an evaluation in a stacktrace
(class) [edit]
approximate representation of an invocation sequence consists of stacktrace entries
(class template) [edit]

[edit] Debugging support

Defined in header <debugging>
pauses the running program when called
(function) [edit]
calls std::breakpoint if std::is_debugger_present returns true
(function) [edit]
checks whether a program is running under the control of a debugger
(function) [edit]

[edit] See also

static_assert declaration (C++11) performs compile-time assertion checking[edit]
C documentation for Error handling