ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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#include <typeinfo>
29#include <type_traits>
30
31namespace itk
32{
33
34class LightObject;
35
55class ITKCommon_EXPORT ExceptionObject : public std::exception
56{
57public:
58 static constexpr const char * const default_exception_message = "Generic ExceptionObject";
59 using Superclass = std::exception;
60
62 ExceptionObject() noexcept = default;
63
64 explicit ExceptionObject(std::string file,
65 unsigned int lineNumber,
66 std::string description,
67 std::string location,
68 const LightObject * thrower);
69
70 explicit ExceptionObject(std::string file,
71 unsigned int lineNumber = 0,
72 std::string description = "None",
73 std::string location = {},
74 const void * thrower = nullptr);
75
77 ExceptionObject(const ExceptionObject &) noexcept = default;
78
80 ExceptionObject(ExceptionObject &&) noexcept = default;
81
84 operator=(const ExceptionObject &) noexcept = default;
85
88 operator=(ExceptionObject &&) noexcept = default;
89
92 ~ExceptionObject() override;
93
95 virtual bool
96 operator==(const ExceptionObject & orig) const;
97
99
101 itkVirtualGetNameOfClassMacro(ExceptionObject);
102
107 virtual void
108 Print(std::ostream & os) const;
109
113 virtual void
114 SetLocation(const std::string & s);
115
116 virtual void
117 SetDescription(const std::string & s);
118
119 virtual void
120 SetLocation(const char * s);
121
122 virtual void
123 SetDescription(const char * s);
124
125
126 [[nodiscard]] virtual const char *
127 GetLocation() const;
128
129 [[nodiscard]] virtual const char *
131
133 [[nodiscard]] virtual const char *
134 GetFile() const;
135
137 [[nodiscard]] virtual unsigned int
138 GetLine() const;
139
141 [[nodiscard]] const char *
142 what() const noexcept override;
143
144private:
145 class ExceptionData; // Defined in .cxx
146
147 std::shared_ptr<const ExceptionData> m_ExceptionData;
148};
149
151inline std::ostream &
152operator<<(std::ostream & os, const ExceptionObject & e)
153{
154 e.Print(os);
155 return os;
156}
157
161
167class ITKCommon_EXPORT MemoryAllocationError : public ExceptionObject
168{
169public:
170 // Inherit the constructors from its base class.
172
174 itkOverrideGetNameOfClassMacro(MemoryAllocationError);
175};
176
182class ITKCommon_EXPORT RangeError : public ExceptionObject
183{
184public:
185 // Inherit the constructors from its base class.
187
189 itkOverrideGetNameOfClassMacro(RangeError);
190};
191
198class ITKCommon_EXPORT InvalidArgumentError : public ExceptionObject
199{
200public:
201 // Inherit the constructors from its base class.
203
205 itkOverrideGetNameOfClassMacro(InvalidArgumentError);
206};
207
213class ITKCommon_EXPORT IncompatibleOperandsError : public ExceptionObject
214{
215public:
216 // Inherit the constructors from its base class.
218
220 itkOverrideGetNameOfClassMacro(IncompatibleOperandsError);
221};
222
228class ITKCommon_EXPORT ProcessAborted : public ExceptionObject
229{
230public:
235 {
236 this->SetDescription("Filter execution was aborted by an external request");
237 }
238
240 ProcessAborted(std::string file, unsigned int lineNumber)
241 : ExceptionObject(std::move(file), lineNumber, "Filter execution was aborted by an external request")
242 {}
243
245 itkOverrideGetNameOfClassMacro(ProcessAborted);
246};
247
248// Forward declaration in Macro.h, implementation here to avoid circular dependency
249template <typename TTarget, typename TSource>
250TTarget
252{
253#ifndef NDEBUG
254 if (x == nullptr)
255 {
256 return nullptr;
257 }
258 auto rval = dynamic_cast<TTarget>(x);
259 if (rval == nullptr)
260 {
261 itkGenericExceptionMacro("Failed dynamic cast to " << typeid(TTarget).name()
262 << " object type = " << x->GetNameOfClass());
263 }
264 return rval;
265#else
266 return static_cast<TTarget>(x);
267#endif
268}
269
270} // end namespace itk
271#endif // itkExceptionObject_h
Standard exception handling object.
virtual const char * GetLocation() const
const char * what() const noexcept override
virtual void SetDescription(const std::string &s)
static constexpr const char *const default_exception_message
virtual void SetLocation(const std::string &s)
std::shared_ptr< const ExceptionData > m_ExceptionData
ExceptionObject() noexcept=default
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ExceptionObject)
virtual const char * GetFile() const
virtual void Print(std::ostream &os) const
ExceptionObject(ExceptionObject &&) noexcept=default
virtual unsigned int GetLine() const
virtual const char * GetDescription() const
ExceptionObject(const ExceptionObject &) noexcept=default
ExceptionObject() noexcept=default
ExceptionObject() noexcept=default
Light weight base class for most itk classes.
ExceptionObject() noexcept=default
ProcessAborted(std::string file, unsigned int lineNumber)
ExceptionObject() noexcept=default
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
TTarget itkDynamicCastInDebugMode(TSource x)
STL namespace.