BDE 4.14.0 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.
136
137 // CLASS METHODS
138
139 /// Throw a `bsl::bad_alloc` exception if exceptions are enabled in the
140 /// current build mode, otherwise abort the program. `bsl::bad_alloc`
141 /// will be an alias for the platform's `std::bad_alloc` class, to
142 /// ensure both ABI and API compatibility with non-BDE code relying on
143 /// the standard exception hierarchy.
145 static void throwBadAlloc();
146
147 /// Throw a `bsl::bad_cast` exception if exceptions are enabled in the
148 /// current build mode, otherwise abort the program. `bsl::bad_cast`
149 /// will be an alias for the platform's `std::bad_cast` class, to ensure
150 /// both ABI and API compatibility with non-BDE code relying on the
151 /// standard exception hierarchy.
153 static void throwBadCast();
154
155 /// Throw a `bsl::bad_exception` exception if exceptions are enabled in
156 /// the current build mode, otherwise abort the program.
157 /// `bsl::bad_exception` will be an alias for the platform's
158 /// `std::bad_exception` class, to ensure both ABI and API compatibility
159 /// with non-BDE code relying on the standard exception hierarchy.
161 static void throwBadException();
162
163 /// Throw a `bsl::bad_typeid` exception if exceptions are enabled in the
164 /// current build mode, otherwise abort the program. `bsl::bad_typeid`
165 /// will be an alias for the platform's `std::bad_typeid` class, to
166 /// ensure both ABI and API compatibility with non-BDE code relying on
167 /// the standard exception hierarchy.
169 static void throwBadTypeid();
170
171 /// Throw a `bsl::exception` exception if exceptions are enabled in the
172 /// current build mode, otherwise abort the program. `bsl::exception`
173 /// will be an alias for the platform's `std::exception` class, to
174 /// ensure both ABI and API compatibility with non-BDE code relying on
175 /// the standard exception hierarchy.
177 static void throwException();
178};
179
180} // close package namespace
181
182
183#endif
184
185// ----------------------------------------------------------------------------
186// Copyright 2013 Bloomberg Finance L.P.
187//
188// Licensed under the Apache License, Version 2.0 (the "License");
189// you may not use this file except in compliance with the License.
190// You may obtain a copy of the License at
191//
192// http://www.apache.org/licenses/LICENSE-2.0
193//
194// Unless required by applicable law or agreed to in writing, software
195// distributed under the License is distributed on an "AS IS" BASIS,
196// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
197// See the License for the specific language governing permissions and
198// limitations under the License.
199// ----------------------------- END-OF-FILE ----------------------------------
200
201/** @} */
202/** @} */
203/** @} */
#define BSLS_ANNOTATION_NORETURN
Definition bsls_annotation.h:383
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlt_iso8601util.h:691
Definition bsls_bslexceptionutil.h:135
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()