BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bsls_bslexceptionutil.h
Go to the documentation of this file.
1/// @file bsls_bslexceptionutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_bslexceptionutil.h -*-C++-*-
8#ifndef INCLUDED_BSLS_BSLEXCEPTIONUTIL
9#define INCLUDED_BSLS_BSLEXCEPTIONUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_bslexceptionutil bsls_bslexceptionutil
15/// @brief Provide functions for use in `bsl` that throw standard exceptions.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_bslexceptionutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_bslexceptionutil-purpose"> Purpose</a>
25/// * <a href="#bsls_bslexceptionutil-classes"> Classes </a>
26/// * <a href="#bsls_bslexceptionutil-description"> Description </a>
27/// * <a href="#bsls_bslexceptionutil-usage"> Usage </a>
28/// * <a href="#bsls_bslexceptionutil-example-1-throwing-a-standard-exception"> Example 1: Throwing a standard exception </a>
29///
30/// # Purpose {#bsls_bslexceptionutil-purpose}
31/// Provide functions for use in `bsl` that throw standard exceptions.
32///
33/// # Classes {#bsls_bslexceptionutil-classes}
34///
35/// - bsls::BslExceptionUtil: namespace for utilities to throw exceptions
36///
37/// @see bsl_exception, bsl_new, bsl_typeinfo
38///
39/// # Description {#bsls_bslexceptionutil-description}
40/// This component provides a means to throw standard exceptions
41/// without introducing a compile-time dependency on the standard exception
42/// classes. This is valuable where header files define function templates or
43/// inline functions that may throw these types as exceptions.
44///
45/// ## Usage {#bsls_bslexceptionutil-usage}
46///
47///
48/// This section illustrates intended use of this component.
49///
50/// ### Example 1: Throwing a standard exception {#bsls_bslexceptionutil-example-1-throwing-a-standard-exception}
51///
52///
53/// Suppose we are implementing a class that must conform to the requirements of
54/// the C++ Standard. There are several clauses that dictate throwing an
55/// exception of a standard type to indicate failure. However, we do not want
56/// to expose the standard exception header to our clients, which would be
57/// typical when implementing function templates inline, and we want to have a
58/// consistent behavior when building with a compiler in a non-standard mode
59/// that does not support exceptions.
60///
61/// First we declare a function template that wants to throw a standard
62/// exception. Note that the `exception` header is not included at this point.
63/// @code
64/// #include <bsls_bslexceptionutil.h>
65///
66/// template<typename T>
67/// void testFunction(int selector)
68/// // Throw a standard exception according to the specified 'selector'.
69/// {
70/// switch (selector) {
71/// @endcode
72/// Now we can use the utilities in this component to throw the desired
73/// exception, even though the standard exception classes are not visible to
74/// this code.
75/// @code
76/// case 1: bsls::BslExceptionUtil::throwBadAlloc();
77/// case 2: bsls::BslExceptionUtil::throwBadCast();
78/// default: bsls::BslExceptionUtil::throwException();
79/// }
80/// }
81/// @endcode
82/// Finally, we can write some client code that calls our function, and wishes
83/// to catch the thrown exception. Observe that this file must #include the
84/// corresponding standard header in order to catch the exception.
85/// @code
86/// #include <exception>
87/// #include <new>
88/// #include <typeinfo>
89///
90/// void callTestFunction()
91/// {
92/// try {
93/// testFunction<int>(1);
94/// assert(0 == "Should throw before reaching here.");
95/// }
96/// catch (const std::bad_alloc& ex) {
97/// }
98///
99/// try {
100/// testFunction<double>(2);
101/// assert(0 == "Should throw before reaching here.");
102/// }
103/// catch (const std::bad_cast& ex) {
104/// }
105/// }
106/// @endcode
107/// @}
108/** @} */
109/** @} */
110
111/** @addtogroup bsl
112 * @{
113 */
114/** @addtogroup bsls
115 * @{
116 */
117/** @addtogroup bsls_bslexceptionutil
118 * @{
119 */
120
121#include <bsls_annotation.h>
123#include <bsls_platform.h>
124
125
126
127namespace bsls {
128
129 //=======================
130 // class BslExceptionUtil
131 //=======================
132
133/// This `struct` provides a namespace for `static` utility functions that
134/// throw standard library exceptions.
135///
136/// See @ref bsls_bslexceptionutil
138
139 // CLASS METHODS
140
141 /// Throw a `bsl::bad_alloc` exception if exceptions are enabled in the
142 /// current build mode, otherwise abort the program. `bsl::bad_alloc`
143 /// will be an alias for the platform's `std::bad_alloc` class, to
144 /// ensure both ABI and API compatibility with non-BDE code relying on
145 /// the standard exception hierarchy.
147 static void throwBadAlloc();
148
149 /// Throw a `bsl::bad_cast` exception if exceptions are enabled in the
150 /// current build mode, otherwise abort the program. `bsl::bad_cast`
151 /// will be an alias for the platform's `std::bad_cast` class, to ensure
152 /// both ABI and API compatibility with non-BDE code relying on the
153 /// standard exception hierarchy.
155 static void throwBadCast();
156
157 /// Throw a `bsl::bad_exception` exception if exceptions are enabled in
158 /// the current build mode, otherwise abort the program.
159 /// `bsl::bad_exception` will be an alias for the platform's
160 /// `std::bad_exception` class, to ensure both ABI and API compatibility
161 /// with non-BDE code relying on the standard exception hierarchy.
163 static void throwBadException();
164
165 /// Throw a `bsl::bad_typeid` exception if exceptions are enabled in the
166 /// current build mode, otherwise abort the program. `bsl::bad_typeid`
167 /// will be an alias for the platform's `std::bad_typeid` class, to
168 /// ensure both ABI and API compatibility with non-BDE code relying on
169 /// the standard exception hierarchy.
171 static void throwBadTypeid();
172
173 /// Throw a `bsl::exception` exception if exceptions are enabled in the
174 /// current build mode, otherwise abort the program. `bsl::exception`
175 /// will be an alias for the platform's `std::exception` class, to
176 /// ensure both ABI and API compatibility with non-BDE code relying on
177 /// the standard exception hierarchy.
179 static void throwException();
180};
181
182} // close package namespace
183
184
185#endif
186
187// ----------------------------------------------------------------------------
188// Copyright 2013 Bloomberg Finance L.P.
189//
190// Licensed under the Apache License, Version 2.0 (the "License");
191// you may not use this file except in compliance with the License.
192// You may obtain a copy of the License at
193//
194// http://www.apache.org/licenses/LICENSE-2.0
195//
196// Unless required by applicable law or agreed to in writing, software
197// distributed under the License is distributed on an "AS IS" BASIS,
198// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
199// See the License for the specific language governing permissions and
200// limitations under the License.
201// ----------------------------- END-OF-FILE ----------------------------------
202
203/** @} */
204/** @} */
205/** @} */
#define BSLS_ANNOTATION_NORETURN
Definition bsls_annotation.h:378
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlt_iso8601util.h:707
Definition bsls_bslexceptionutil.h:137
static BSLS_ANNOTATION_NORETURN void throwBadTypeid()
static BSLS_ANNOTATION_NORETURN void throwBadAlloc()
static BSLS_ANNOTATION_NORETURN void throwBadCast()
static BSLS_ANNOTATION_NORETURN void throwBadException()
static BSLS_ANNOTATION_NORETURN void throwException()