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 
29 namespace itk
30 {
50 class ITKCommon_EXPORT ExceptionObject : public std::exception
51 {
52 public:
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 
133 private:
134  class ExceptionData;
135 
136  std::shared_ptr<const ExceptionData> m_ExceptionData{};
137 };
138 
140 inline std::ostream &
141 operator<<(std::ostream & os, const ExceptionObject & e)
142 {
143  e.Print(os);
144  return os;
145 }
157 class ITKCommon_EXPORT MemoryAllocationError : public ExceptionObject
158 {
159 public:
160  // Inherit the constructors from its base class.
162 
164  itkOverrideGetNameOfClassMacro(MemoryAllocationError);
165 };
173 class ITKCommon_EXPORT RangeError : public ExceptionObject
174 {
175 public:
176  // Inherit the constructors from its base class.
178 
180  itkOverrideGetNameOfClassMacro(RangeError);
181 };
190 class ITKCommon_EXPORT InvalidArgumentError : public ExceptionObject
191 {
192 public:
193  // Inherit the constructors from its base class.
195 
197  itkOverrideGetNameOfClassMacro(InvalidArgumentError);
198 };
206 class ITKCommon_EXPORT IncompatibleOperandsError : public ExceptionObject
207 {
208 public:
209  // Inherit the constructors from its base class.
211 
213  itkOverrideGetNameOfClassMacro(IncompatibleOperandsError);
214 };
222 class ITKCommon_EXPORT ProcessAborted : public ExceptionObject
223 {
224 public:
228  : ExceptionObject()
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
253 template <typename TTarget, typename TSource>
254 TTarget
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
itk::itkDynamicCastInDebugMode
TTarget itkDynamicCastInDebugMode(TSource x)
Definition: itkExceptionObject.h:255
itk::ProcessAborted
Definition: itkExceptionObject.h:222
itk::ProcessAborted::ProcessAborted
ProcessAborted()
Definition: itkExceptionObject.h:227
itk::RangeError
Definition: itkExceptionObject.h:173
itk::InvalidArgumentError
Definition: itkExceptionObject.h:190
itk::ProcessAborted::ProcessAborted
ProcessAborted(const char *file, unsigned int lineNumber)
Definition: itkExceptionObject.h:234
itk::ExceptionObject::ExceptionObject
ExceptionObject() noexcept=default
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::ProcessAborted::ProcessAborted
ProcessAborted(const std::string &file, unsigned int lineNumber)
Definition: itkExceptionObject.h:241
itk::IncompatibleOperandsError
Definition: itkExceptionObject.h:206
itk::ExceptionObject
Standard exception handling object.
Definition: itkExceptionObject.h:50
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::MemoryAllocationError
Definition: itkExceptionObject.h:157
itk::Math::e
static constexpr double e
Definition: itkMath.h:56
itk::ExceptionObject::Superclass
std::exception Superclass
Definition: itkExceptionObject.h:54