ITK  6.0.0
Insight Toolkit
itkExceptionObject.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkExceptionObject_h
19#define itkExceptionObject_h
20
21// NOTE: This itkExceptionObject.h file is included by itkMacro.h, and should never be included directly.
22#ifndef allow_inclusion_of_itkExceptionObject_h
23# error "Do not include itkExceptionObject.h directly, include itkMacro.h instead."
24#endif
25
26#include <memory> // For shared_ptr.
27#include <string>
28
29namespace itk
30{
50class ITKCommon_EXPORT ExceptionObject : public std::exception
51{
52public:
53 static constexpr const char * const default_exception_message = "Generic ExceptionObject";
54 using Superclass = std::exception;
55
57 ExceptionObject() noexcept = default;
58
59 explicit ExceptionObject(const char * file,
60 unsigned int lineNumber = 0,
61 const char * desc = "None",
62 const char * loc = "Unknown");
63 explicit ExceptionObject(std::string file,
64 unsigned int lineNumber = 0,
65 std::string desc = "None",
66 std::string loc = "Unknown");
67
69 ExceptionObject(const ExceptionObject &) noexcept = default;
70
72 ExceptionObject(ExceptionObject &&) noexcept = default;
73
76 operator=(const ExceptionObject &) noexcept = default;
77
80 operator=(ExceptionObject &&) noexcept = default;
81
84 ~ExceptionObject() override;
85
87 virtual bool
88 operator==(const ExceptionObject & orig) const;
89
91 itkVirtualGetNameOfClassMacro(ExceptionObject);
92
97 virtual void
98 Print(std::ostream & os) const;
99
103 virtual void
104 SetLocation(const std::string & s);
105
106 virtual void
107 SetDescription(const std::string & s);
108
109 virtual void
110 SetLocation(const char * s);
111
112 virtual void
113 SetDescription(const char * s);
114
115 virtual const char *
116 GetLocation() const;
117
118 virtual const char *
119 GetDescription() const;
120
122 virtual const char *
123 GetFile() const;
124
126 virtual unsigned int
127 GetLine() const;
128
130 const char *
131 what() const noexcept override;
132
133private:
134 class ExceptionData;
135
136 std::shared_ptr<const ExceptionData> m_ExceptionData{};
137};
138
140inline std::ostream &
141operator<<(std::ostream & os, const ExceptionObject & e)
142{
143 e.Print(os);
144 return os;
145}
157class ITKCommon_EXPORT MemoryAllocationError : public ExceptionObject
158{
159public:
160 // Inherit the constructors from its base class.
162
164 itkOverrideGetNameOfClassMacro(MemoryAllocationError);
165};
173class ITKCommon_EXPORT RangeError : public ExceptionObject
174{
175public:
176 // Inherit the constructors from its base class.
178
180 itkOverrideGetNameOfClassMacro(RangeError);
181};
190class ITKCommon_EXPORT InvalidArgumentError : public ExceptionObject
191{
192public:
193 // Inherit the constructors from its base class.
195
197 itkOverrideGetNameOfClassMacro(InvalidArgumentError);
198};
206class ITKCommon_EXPORT IncompatibleOperandsError : public ExceptionObject
207{
208public:
209 // Inherit the constructors from its base class.
211
213 itkOverrideGetNameOfClassMacro(IncompatibleOperandsError);
214};
222class ITKCommon_EXPORT ProcessAborted : public ExceptionObject
223{
224public:
229 {
230 this->SetDescription("Filter execution was aborted by an external request");
231 }
232
234 ProcessAborted(const char * file, unsigned int lineNumber)
235 : ExceptionObject(file, lineNumber)
236 {
237 this->SetDescription("Filter execution was aborted by an external request");
238 }
239
241 ProcessAborted(const std::string & file, unsigned int lineNumber)
242 : ExceptionObject(file, lineNumber)
243 {
244 this->SetDescription("Filter execution was aborted by an external request");
245 }
246
248 itkOverrideGetNameOfClassMacro(ProcessAborted);
249};
252// Forward declaration in Macro.h, implementation here to avoid circular dependency
253template <typename TTarget, typename TSource>
254TTarget
256{
257#ifndef NDEBUG
258 if (x == nullptr)
259 {
260 return nullptr;
261 }
262 TTarget rval = dynamic_cast<TTarget>(x);
263 if (rval == nullptr)
264 {
265 itkGenericExceptionMacro("Failed dynamic cast to " << typeid(TTarget).name()
266 << " object type = " << x->GetNameOfClass());
267 }
268 return rval;
269#else
270 return static_cast<TTarget>(x);
271#endif
272}
273
274} // end namespace itk
275#endif // itkExceptionObject_h
Standard exception handling object.
ExceptionObject() noexcept=default
std::exception Superclass
ProcessAborted(const std::string &file, unsigned int lineNumber)
ProcessAborted(const char *file, unsigned int lineNumber)
static constexpr double e
Definition: itkMath.h:56
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
TTarget itkDynamicCastInDebugMode(TSource x)
STL namespace.