BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_systemerror.h
Go to the documentation of this file.
1/// @file bslstl_systemerror.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_systemerror.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_SYSTEMERROR
9#define INCLUDED_BSLSTL_SYSTEMERROR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14// BDE_VERIFY pragma: -TP25 // CLASSES are not defined in C++11
15
16/// @defgroup bslstl_systemerror bslstl_systemerror
17/// @brief Provide a standard compliant `system_error` class.
18/// @addtogroup bsl
19/// @{
20/// @addtogroup bslstl
21/// @{
22/// @addtogroup bslstl_systemerror
23/// @{
24///
25/// <h1> Outline </h1>
26/// * <a href="#bslstl_systemerror-purpose"> Purpose</a>
27/// * <a href="#bslstl_systemerror-classes"> Classes </a>
28/// * <a href="#bslstl_systemerror-canonical-header"> Canonical Header </a>
29/// * <a href="#bslstl_systemerror-description"> Description </a>
30/// * <a href="#bslstl_systemerror-usage"> Usage </a>
31/// * <a href="#bslstl_systemerror-example-1-adding-annotation-to-an-error"> Example 1: Adding Annotation to an Error </a>
32///
33/// # Purpose {#bslstl_systemerror-purpose}
34/// Provide a standard compliant @ref system_error class.
35///
36/// # Classes {#bslstl_systemerror-classes}
37///
38/// - bsl::system_error: a standard compliant @ref system_error class
39///
40/// # Canonical Header {#bslstl_systemerror-canonical-header}
41/// bsl_system_error.h
42///
43/// # Description {#bslstl_systemerror-description}
44/// This component defines class `bsl::system_error`, a class used
45/// for annotated exception objects about `errno`-style errors. In C++11 mode,
46/// the vendor-supplied `<system_error>` implementation is used instead, and the
47/// corresponding names from `std` are imported into `bsl`.
48///
49/// ## Usage {#bslstl_systemerror-usage}
50///
51///
52/// In this section we show intended use of this component.
53///
54/// ### Example 1: Adding Annotation to an Error {#bslstl_systemerror-example-1-adding-annotation-to-an-error}
55///
56///
57/// Suppose we want to add an informative message when a system error occurs and
58/// include that as part of an exception that we throw when reporting the error.
59/// We can use `bsl::system_error` to do that.
60///
61/// First, reset `errno` to avoid detecting old problems.
62/// @code
63/// errno = 0;
64/// @endcode
65/// Then, do something that will fail and set `errno`.
66/// @code
67/// strtod("1e2000", 0);
68/// @endcode
69/// Next, check that `errno` was actually set.
70/// @code
71/// assert(ERANGE == errno);
72/// @endcode
73/// Finally, prepare an annotated exception and verify the annotation and the
74/// error code stored within it.
75/// @code
76/// bsl::system_error annotated(errno, generic_category(), "1e2000");
77/// assert(strstr(annotated.what(), "1e2000"));
78/// assert(static_cast<int>(bsl::errc::result_out_of_range) ==
79/// annotated.code().value());
80/// assert(&generic_category() == &annotated.code().category());
81/// @endcode
82/// @}
83/** @} */
84/** @} */
85
86/** @addtogroup bsl
87 * @{
88 */
89/** @addtogroup bslstl
90 * @{
91 */
92/** @addtogroup bslstl_systemerror
93 * @{
94 */
95
96#include <bslscm_version.h>
97
99
100#include <bslstl_errc.h>
101#include <bslstl_error.h>
102
103#include <errno.h>
104
105#include <cstring>
106#include <functional>
107#include <stdexcept>
108#include <string>
109
110#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
111#include <bsls_nativestd.h>
112#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
113
114#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
115
116#include <system_error>
117
118namespace bsl {
119using std::system_error;
120} // close namespace bsl
121
122#else
123
124namespace bsl {
125
126 // ==================
127 // class system_error
128 // ==================
129
130/// This class represents exceptions that have an associated error code.
131///
132/// See @ref bslstl_systemerror
133class system_error : public std::runtime_error {
134
135 public:
136 // CREATORS
137
138 /// Create an object of this type holding the specified `code`.
139 /// Optionally specify a string `what` to be added to the description of
140 /// this object.
141 system_error(error_code code, const std::string& what);
142 system_error(error_code code, const char *what);
143 system_error(error_code code); // IMPLICIT
144
145 /// Create an object of this type holding an error code holding the
146 /// specified `value` and `category`. Optionally specify a string
147 /// `what` to be added to the description of this object.
148 system_error(int value,
149 const error_category& category,
150 const std::string& what);
151 system_error(int value, const error_category& category, const char *what);
152 system_error(int value, const error_category& category);
153
154 // ACCESSORS
155
156 /// Return a `const` reference to the error code held by this object.
157 const error_code& code() const;
158
159 private:
160 // DATA
161 error_code d_code; // error code
162};
163
164// ============================================================================
165// INLINE DEFINITIONS
166// ============================================================================
167
168 // ------------------
169 // class system_error
170 // ------------------
171
172// CREATORS
173inline
174system_error::system_error(error_code code, const std::string& what)
175: std::runtime_error(what + std::string(": ") + code.message())
176, d_code(code)
177{
178}
179
180inline
182: std::runtime_error(what + std::string(": ") + code.message())
183, d_code(code)
184{
185}
186
187inline
189: std::runtime_error(code.message())
190, d_code(code)
191{
192}
193
194inline
196 const error_category& category,
197 const std::string& what)
198: std::runtime_error(what + std::string(": ") +
199 category.message(value))
200, d_code(value, category)
201{
202}
203
204inline
206 const error_category& category,
207 const char *what)
208: std::runtime_error(what + std::string(": ") +
209 category.message(value))
210, d_code(value, category)
211{
212}
213
214inline
216: std::runtime_error(category.message(value))
217, d_code(value, category)
218{
219}
220
221// ACCESSORS
222inline
224{
225 return d_code;
226}
227
228} // close namespace bsl
229
230#endif
231#endif
232
233// ----------------------------------------------------------------------------
234// Copyright 2019 Bloomberg Finance L.P.
235//
236// Licensed under the Apache License, Version 2.0 (the "License");
237// you may not use this file except in compliance with the License.
238// You may obtain a copy of the License at
239//
240// http://www.apache.org/licenses/LICENSE-2.0
241//
242// Unless required by applicable law or agreed to in writing, software
243// distributed under the License is distributed on an "AS IS" BASIS,
244// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
245// See the License for the specific language governing permissions and
246// limitations under the License.
247// ----------------------------- END-OF-FILE ----------------------------------
248
249/** @} */
250/** @} */
251/** @} */
Definition bslstl_string.h:1252
Definition bslstl_error.h:157
Definition bslstl_error.h:214
Definition bslstl_systemerror.h:133
system_error(error_code code, const std::string &what)
Definition bslstl_systemerror.h:174
const error_code & code() const
Return a const reference to the error code held by this object.
Definition bslstl_systemerror.h:223
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939
Definition bdldfp_decimal.h:5549