Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error"

From cppreference.com
< cpp
(+ c++11 mark)
(missing items)
Line 1: Line 1:
 
{{title|Error handling}}
 
{{title|Error handling}}
 
{{cpp/error/sidebar}}
 
{{cpp/error/sidebar}}
===Exceptions===
 
  
The class {{rlt | exception/exception | exception}} provides a common interface to signal for error conditions through the [[cpp/language/throw | throw expression]].
+
===Exception handling===
 +
The header {{tt|<exception>}} provide several classes and functions related to exception handling in C++ programs.
 +
 
 +
{{dcl list begin}}
 +
{{dcl list header | exception}}
 +
{{dcl list class | cpp/error/exception/exception | base class for exceptions thrown by the standard library components}}
 +
{{dcl list 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}}}}
 +
{{dcl list typedef | cpp/error/exception/exception_ptr | shared pointer type for handling exception objects | notes={{mark c++11}}}}
 +
{{dcl list tfun | cpp/error/exception/make_exception_ptr | creates an {{cpp|std::exception_ptr}} from an exception object| notes={{mark c++11}}}}
 +
{{dcl list fun | cpp/error/exception/current_exception | captures the current exception in a {{cpp|std::exception_ptr}}| notes={{mark c++11}}}}
 +
{{dcl list fun | cpp/error/exception/rethrow_exception | throws the exception from an {{cpp|std::exception_ptr}}| notes={{mark c++11}}}}
 +
{{dcl list class | cpp/error/exception/nested_exception | a mixin type to capture and store current exceptions| notes={{mark c++11}}}}
 +
{{dcl list tfun | cpp/error/exception/throw_with_nested | throws its argument with {{cpp|std::nested_exception}} mixed in| notes={{mark c++11}}}}
 +
{{dcl list tfun | cpp/error/exception/rethrow_if_nested | throws the exception from a {{cpp|std::nested_exception}}| notes={{mark c++11}}}}
 +
{{dcl list h2 | Handling of failures in exception handling}}
 +
{{dcl list fun | cpp/error/exception/terminate | function called when exception handling fails}}
 +
{{dcl list typedef | cpp/error/exception/terminate_handler | the type of the function called by {{cpp|std::terminate}}}}
 +
{{dcl list fun | cpp/error/exception/get_terminate | obtains the current terminate_handler}}
 +
{{dcl list fun | cpp/error/exception/set_terminate | changes the function to be called by {{cpp|std::terminate}} }}
 +
{{dcl list h2 | Handling of exception specification violations}}
 +
{{dcl list fun | cpp/error/exception/unexpected | notes={{mark deprecated}} | function called when dynamic exception specification is violated}}
 +
{{dcl list class | cpp/error/exception/bad_exception | notes={{mark deprecated}} | exception thrown when dynamic exception specification is violated, if possible}}
 +
{{dcl list typedef | cpp/error/exception/unexpected_handler | notes={{mark deprecated}} |  the type of the function called by {{cpp|std::unexpected}} }}
 +
{{dcl list fun | cpp/error/exception/get_unexpected | notes={{mark deprecated}} | obtains the current unexpected_handler}}
 +
{{dcl list fun | cpp/error/exception/set_unexpected | notes={{mark deprecated}} | changes the function to be called by {{cpp|std::unexpected}}}}
 +
{{dcl list end}}
  
 
====Exception categories====
 
====Exception categories====
  
Several convenience classes are predefined 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.
+
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.
  
*{{ltt|cpp/error/exception/logic_error}}
+
{{dcl list begin}}
:*{{ltt|cpp/error/exception/invalid_argument}}
+
{{dcl list header | stdexcept}}
:*{{ltt|cpp/error/exception/domain_error}}
+
{{dcl list class | cpp/error/exception/logic_error | exception class to indicate violations of logical preconditions or class invariants}}
:*{{ltt|cpp/error/exception/length_error}}
+
{{dcl list class | cpp/error/exception/invalid_argument | exception class to report invalid arguments}}
:*{{ltt|cpp/error/exception/out_of_range}}
+
{{dcl list class | cpp/error/exception/domain_error | exception class to report domain errors}}
*{{ltt|cpp/error/exception/runtime_error}}
+
{{dcl list class | cpp/error/exception/length_error | exception class to report attempts to exceed maximum allowed size}}
:*{{ltt|cpp/error/exception/range_error}}
+
{{dcl list class | cpp/error/exception/out_of_range | exception class to report arguments outside of expected range}}
:*{{ltt|cpp/error/exception/overflow_error}}
+
{{dcl list class | cpp/error/exception/runtime_error | exception class to indicate conditions only detectable at run time}}
:*{{ltt|cpp/error/exception/underflow_error}}
+
{{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}}
  
 
===Error numbers===
 
===Error numbers===
  
{{rl | errno | Several macros}} are available, that provide POSIX error number support. An {{tt|errno}} variable is also supported, a separate value is given to each thread.{{mark c++11 feature}}
+
{{dcl list begin}}
 +
{{dcl list header | cerrno}}
 +
{{dcl list macro const | cpp/error/errno | macro which expands to POSIX-compatible thread-local error number variable}}
 +
{{dcl list macro const | cpp/error/errno_macros | title=E2BIG, EACCES, ..., EXDEV | 78 macros for standard POSIX-compatible error conditions}}
 +
{{dcl list end}}
  
 
===Assertions===
 
===Assertions===
Line 29: Line 61:
 
{{dcl list begin}}
 
{{dcl list begin}}
 
{{dcl list header | cassert}}
 
{{dcl list header | cassert}}
 +
{{dcl list keyword | cpp/keywords/static_assert | enders the program ill-formed if the user-specified condition is not {{cpp|true}} at compile time| notes={{mark c++11}}}}
 
{{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}}
 
{{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}}
 
{{dcl list end}}
 
{{dcl list end}}
 
{{ltt|cpp/language/static_assert}} declaration renders the program ill-formed if the user-specified condition is not {{cpp|true}} {{mark c++11 feature}}.
 
  
 
===System error===
 
===System error===
  
{{todo}}
+
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}}
 +
{{dcl list header | system_error}}
 +
{{dcl list class  | cpp/error/system_error/error_category | base class for error categories | notes={{mark c++11}}}}
 +
{{dcl list class  | cpp/error/system_error/generic_category | error category indicating generic error| notes={{mark c++11}}}}
 +
{{dcl list class  | cpp/error/system_error/system_category | error category indicating the operating system as the source of error| notes={{mark c++11}}}}
 +
 
 +
{{dcl list class  | cpp/error/system_error/error_condition | identifies an error condition| notes={{mark c++11}}}}
 +
{{dcl list fun  | cpp/error/system_error/make_error_condition | creates an error condition object of specified error_category| notes={{mark c++11}}}}
 +
{{dcl list tclass  | cpp/error/system_error/is_error_condition_enum | identifies an enumeration as an {{cpp|std::error_condition}}| notes={{mark c++11}}}}
 +
{{dcl list class  | cpp/error/system_error/err_c | the {{cpp|std::error_condition}} enumeration listing all 78 standard cerrno macro constants| notes={{mark c++11}}}}
 +
 
 +
{{dcl list class  | cpp/error/system_error/error_code | holds error code values| notes={{mark c++11}}}}
 +
{{dcl list fun  | cpp/error/system_error/make_error_code | creates an error code from an error_category| notes={{mark c++11}}}}
 +
{{dcl list tclass  | cpp/error/system_error/is_error_code_enum | identifies a class as an error_code enumeration| notes={{mark c++11}}}}
 +
 
 +
{{dcl list class  | cpp/error/system_error/system_error | exception class used to report conditions that have an error_code| notes={{mark c++11}}}}
 +
{{dcl list end}}

Revision as of 14:01, 2 November 2011

Template:cpp/error/sidebar

Contents

Exception handling

The header <exception> provide 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)
Capture and storage of exception objects
checks if exception handling is currently in progress
(function)
shared pointer type for handling exception objects
(typedef)
creates an Template:cpp from an exception object
(function template)
captures the current exception in a Template:cpp
(function)
throws the exception from an Template:cpp
(function)
a mixin type to capture and store current exceptions
(class)
throws its argument with Template:cpp mixed in
(function template)
throws the exception from a Template:cpp
(function template)
Handling of failures in exception handling
function called when exception handling fails
(function)
the type of the function called by Template:cpp
(typedef)
obtains the current terminate_handler
(function)
changes the function to be called by Template:cpp
(function)
Handling of exception specification violations
(deprecated)
function called when dynamic exception specification is violated
(function)
(deprecated)
exception thrown when dynamic exception specification is violated, if possible
(class)
the type of the function called by Template:cpp
(typedef)
(deprecated)
obtains the current unexpected_handler
(function)
(deprecated)
changes the function to be called by Template:cpp
(function)

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 can not be easily predicted.

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

Error numbers

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

Assertions

Assertions help to implement checking of preconditions in programs.

Template:dcl list keyword
Defined in header <cassert>
aborts the program if the user-specified condition is not Template:cpp. May be disabled for release builds
(function macro)

System error

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

Defined in header <system_error>
base class for error categories
(class)
error category indicating generic error
(class)
error category indicating the operating system as the source of error
(class)
identifies an error condition
(class)
creates an error condition object of specified error_category
(function)
identifies an enumeration as an Template:cpp
(class template)
(C++11)
the Template:cpp enumeration listing all 78 standard cerrno macro constants
(class)
holds error code values
(class)
creates an error code from an error_category
(function)
identifies a class as an error_code enumeration
(class template)
exception class used to report conditions that have an error_code
(class)