BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_stdexceptutil.h
Go to the documentation of this file.
1/// @file bslstl_stdexceptutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_stdexceptutil.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_STDEXCEPTUTIL
9#define INCLUDED_BSLSTL_STDEXCEPTUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_stdexceptutil bslstl_stdexceptutil
15/// @brief Provide a utility to throw standard exceptions.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_stdexceptutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_stdexceptutil-purpose"> Purpose</a>
25/// * <a href="#bslstl_stdexceptutil-classes"> Classes </a>
26/// * <a href="#bslstl_stdexceptutil-description"> Description </a>
27/// * <a href="#bslstl_stdexceptutil-pre-throw-hooks"> Pre-Throw Hooks </a>
28/// * <a href="#bslstl_stdexceptutil-usage"> Usage </a>
29///
30/// # Purpose {#bslstl_stdexceptutil-purpose}
31/// Provide a utility to throw standard exceptions.
32///
33/// # Classes {#bslstl_stdexceptutil-classes}
34///
35/// - bslstl::StdExceptUtil: namespace for utilities to throw standard exceptions
36///
37/// **Canonical header:** bsl_stdexcept.h
38///
39/// @see stdexcept
40///
41/// # Description {#bslstl_stdexceptutil-description}
42/// This component provides a means to throw standard exceptions
43/// without introducing a compile-time dependency on the standard exception
44/// classes. This valuable where header files define function templates or
45/// inline functions that may throw these types as exceptions.
46///
47/// ## Pre-Throw Hooks {#bslstl_stdexceptutil-pre-throw-hooks}
48///
49///
50/// For each exception type supported by this component, there is a "pre throw
51/// hook", a function pointer that is normally null. If that pointer is set to
52/// a function, that function is called prior to the throw. This gives the
53/// client a chance to log a message.
54///
55/// If the pre-throw hook is set to `StdExceptUtil::logCheapStackTrace`, a cheap
56/// stack trace will be logged, enabling the client to use
57/// `/bb/bin/showfunc.tsk` on the cheap stack trace to get a stack trace with
58/// symbols. When running `showfunc.tsk`, pipe the output through `c++filt` to
59/// get demangled symbols.
60///
61/// If the pre-throw hook is set to
62/// `balst::StackTracePrintUtil::logExceptionStackTrace`, a full multi-line
63/// stack trace with symbols will be logged, with, on some platforms, symbol
64/// demangling, line numbers, and source file names. This alternative requires
65/// considerable disk access and is therefore orders of magnitude slower than
66/// the cheap stack trace.
67///
68/// ## Usage {#bslstl_stdexceptutil-usage}
69///
70///
71/// First we declare a function template that wants to throw a standard
72/// exception. Note that the `stdexcept` header is not included at this point.
73/// @code
74/// #include <bslstl_stdexceptutil.h>
75///
76/// template<typename T>
77/// void testFunction(int selector)
78/// // Throw a standard exception according to the specified 'selector'.
79/// {
80/// switch(selector) {
81/// case 1: {
82/// bslstl::StdExceptUtil::throwRuntimeError("sample message 1");
83/// } break;
84/// case 2: {
85/// bslstl::StdExceptUtil::throwLogicError("sample message 2");
86/// } break;
87/// default: {
88/// bslstl::StdExceptUtil::throwInvalidArgument("ERROR");
89/// } break;
90/// }
91/// @endcode
92/// However, if client code wishes to catch the exception, the `.cpp` file must
93/// `#include` the appropriate header.
94/// @code
95/// #include <stdexcept>
96///
97/// void callTestFunction()
98/// {
99/// try {
100/// testFunction<int>(1);
101/// assert(0 == "Should throw before reaching here.");
102/// }
103/// catch(const runtime_error& ex) {
104/// assert(0 == std::strcmp(ex.what(), "sample message 1"));
105/// }
106///
107/// try {
108/// testFunction<double>(2);
109/// assert(0 == "Should throw before reaching here.");
110/// }
111/// catch(const logic_error& ex) {
112/// assert(0 == std::strcmp(ex.what(), "sample message 2"));
113/// }
114/// }
115/// @endcode
116/// @}
117/** @} */
118/** @} */
119
120/** @addtogroup bsl
121 * @{
122 */
123/** @addtogroup bslstl
124 * @{
125 */
126/** @addtogroup bslstl_stdexceptutil
127 * @{
128 */
129
130#include <bslscm_version.h>
131
132#include <bsla_noreturn.h>
133
135
136#include <stddef.h>
137
138
139
140namespace bslstl {
141
142 //====================
143 // class StdExceptUtil
144 //====================
145
146/// This `struct` provides a namespace for `static` utility functions that
147/// throw standard library exceptions.
149
150 // PUBLIC TYPES
151
152 /// This is the type of function pointer that can be set. One such
153 /// static function pointer exists for each exception type supported by
154 /// this component. Functions called to throw exceptions examine their
155 /// respective pointer, and if it's non-null, call it and then throw
156 /// after it returns. Note that it is recommended that the hook
157 /// function log a greppable statement such as "About to throw
158 /// <exceptionName>".
159 typedef void (*PreThrowHook)(const char *exceptionName,
160 const char *message);
161
162 // CLASS METHODS
163
164 static void logCheapStackTrace(const char *exceptionName,
165 const char *message);
166
168 static void setLogicErrorHook( PreThrowHook hook);
172 static void setOutOfRangeHook( PreThrowHook hook);
173 static void setRangeErrorHook( PreThrowHook hook);
174 /// Log "About to throw ", then the specified `exceptionName`, then the
175 /// specified `message`, then log a cheap stack trace with warning
176 /// severity. This function is intended as a candidate for setting to
177 /// the pre-throw hooks. Note that a far slower alternative to this,
178 /// which logs a full, multi-line stack trace with resolved symbols and,
179 /// on many platforms, line numbers and source file names, is
180 /// `balst::StackTracePrintUtil::logExceptionStackTrace`.
182
183 /// Set the pre throw hook for the specified exception type to the
184 /// specified `hook`. If `hook` is passed 0, or if the settor was never
185 /// called, that means that no pre-throw function will be called.
187
188 /// Throw a `std::runtime_error` exception supplying the specified
189 /// `message` as the sole argument to its constructor.
191 static void throwRuntimeError(const char *message);
192
193 /// Throw a `std::logic_error` exception supplying the specified
194 /// `message` as the sole argument to its constructor.
196 static void throwLogicError(const char *message);
197
198 /// Throw a `std::domain_error` exception supplying the specified
199 /// `message` as the sole argument to its constructor.
201 static void throwDomainError(const char *message);
202
203 /// Throw a `std::invalid_argument` exception supplying the specified
204 /// `message` as the sole argument to its constructor.
206 static void throwInvalidArgument(const char *message);
207
208 /// Throw a `std::length_error` exception supplying the specified
209 /// `message` as the sole argument to its constructor.
211 static void throwLengthError(const char *message);
212
213 /// Throw a `std::out_of_range` exception supplying the specified
214 /// `message` as the sole argument to its constructor.
216 static void throwOutOfRange(const char *message);
217
218 /// Throw a `std::range_error` exception supplying the specified
219 /// `message` as the sole argument to its constructor.
221 static void throwRangeError(const char *message);
222
223 /// Throw a `std::overflow_error` exception supplying the specified
224 /// `message` as the sole argument to its constructor.
226 static void throwOverflowError(const char *message);
227
228 /// Throw a `std::underflow_error` exception supplying the specified
229 /// `message` as the sole argument to its constructor.
231 static void throwUnderflowError(const char *message);
232};
233
234} // close package namespace
235
236#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
237// ============================================================================
238// BACKWARD COMPATIBILITY
239// ============================================================================
240
241/// This alias is defined for backward compatibility.
243#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
244
245
246
247#endif
248
249// ----------------------------------------------------------------------------
250// Copyright 2013 Bloomberg Finance L.P.
251//
252// Licensed under the Apache License, Version 2.0 (the "License");
253// you may not use this file except in compliance with the License.
254// You may obtain a copy of the License at
255//
256// http://www.apache.org/licenses/LICENSE-2.0
257//
258// Unless required by applicable law or agreed to in writing, software
259// distributed under the License is distributed on an "AS IS" BASIS,
260// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
261// See the License for the specific language governing permissions and
262// limitations under the License.
263// ----------------------------- END-OF-FILE ----------------------------------
264
265/** @} */
266/** @} */
267/** @} */
#define BSLA_NORETURN
Definition bsla_noreturn.h:169
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
bslstl::StdExceptUtil bslstl_StdExceptUtil
This alias is defined for backward compatibility.
Definition bslstl_stdexceptutil.h:242
Definition bslstl_algorithm.h:82
Definition bslstl_stdexceptutil.h:148
void(* PreThrowHook)(const char *exceptionName, const char *message)
Definition bslstl_stdexceptutil.h:159
static BSLA_NORETURN void throwOverflowError(const char *message)
static void setDomainErrorHook(PreThrowHook hook)
static void setLengthErrorHook(PreThrowHook hook)
static void setRuntimeErrorHook(PreThrowHook hook)
static BSLA_NORETURN void throwDomainError(const char *message)
static BSLA_NORETURN void throwRuntimeError(const char *message)
static void setRangeErrorHook(PreThrowHook hook)
static void setOutOfRangeHook(PreThrowHook hook)
static void setUnderflowErrorHook(PreThrowHook hook)
static void setOverflowErrorHook(PreThrowHook hook)
static BSLA_NORETURN void throwRangeError(const char *message)
static BSLA_NORETURN void throwLengthError(const char *message)
static void setLogicErrorHook(PreThrowHook hook)
static void setInvalidArgumentHook(PreThrowHook hook)
static BSLA_NORETURN void throwUnderflowError(const char *message)
static void logCheapStackTrace(const char *exceptionName, const char *message)
static BSLA_NORETURN void throwInvalidArgument(const char *message)
static BSLA_NORETURN void throwOutOfRange(const char *message)
static BSLA_NORETURN void throwLogicError(const char *message)