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