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