ITK  5.1.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  * http://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 # error "Do not include itkExceptionObject.h directly, include itkMacro.h instead."
20 #else // itkExceptionObject_h
21 
22 # include <string>
23 # include <stdexcept>
24 
25 # include "itkSmartPointer.h"
26 
27 namespace itk
28 {
48 class ITKCommon_EXPORT ExceptionObject : public std::exception
49 {
50 public:
51  static constexpr const char * const default_exception_message = "Generic ExceptionObject";
52  using Superclass = std::exception;
56  ExceptionObject() noexcept;
57  explicit ExceptionObject(const char * file,
58  unsigned int lineNumber = 0,
59  const char * desc = "None",
60  const char * loc = "Unknown");
61  explicit ExceptionObject(std::string file,
62  unsigned int lineNumber = 0,
63  std::string desc = "None",
64  std::string loc = "Unknown");
65  ExceptionObject(const ExceptionObject & orig) noexcept;
67 
69  ~ExceptionObject() noexcept override;
70 
72  ExceptionObject &
73  operator=(const ExceptionObject & orig) noexcept;
74 
76  virtual bool
77  operator==(const ExceptionObject & orig);
78 
79  virtual const char *
80  GetNameOfClass() const
81  {
82  return "ExceptionObject";
83  }
84 
89  virtual void
90  Print(std::ostream & os) const;
91 
95  virtual void
96  SetLocation(const std::string & s);
97 
98  virtual void
99  SetDescription(const std::string & s);
100 
101  virtual void
102  SetLocation(const char * s);
103 
104  virtual void
105  SetDescription(const char * s);
106 
107  virtual const char *
108  GetLocation() const;
109 
110  virtual const char *
111  GetDescription() const;
112 
114  virtual const char *
115  GetFile() const;
116 
118  virtual unsigned int
119  GetLine() const;
120 
122  const char *
123  what() const noexcept override;
124 
125 private:
151  class ReferenceCounterInterface
152  {
153  public:
154  virtual void
155  Register() const = 0;
156 
157  virtual void
158  UnRegister() const = 0;
159 
160  ReferenceCounterInterface();
161  virtual ~ReferenceCounterInterface();
162  };
163  class ExceptionData;
164  class ReferenceCountedExceptionData;
165  SmartPointer<const ReferenceCounterInterface> m_ExceptionData;
166  const ExceptionData *
167  GetExceptionData() const;
168 };
169 
171 inline std::ostream &
172 operator<<(std::ostream & os, const ExceptionObject & e)
173 {
174  (&e)->Print(os);
175  return os;
176 }
178 
188 class ITKCommon_EXPORT MemoryAllocationError : public ExceptionObject
189 {
190 public:
193  MemoryAllocationError() noexcept
194  : ExceptionObject()
195  {}
196 
198  MemoryAllocationError(const char * file, unsigned int lineNumber)
199  : ExceptionObject(file, lineNumber)
200  {}
201 
203  MemoryAllocationError(const std::string & file, unsigned int lineNumber)
204  : ExceptionObject(file, lineNumber)
205  {}
206 
208  MemoryAllocationError(const std::string & file,
209  unsigned int lineNumber,
210  const std::string & desc,
211  const std::string & loc)
212  : ExceptionObject(file, lineNumber, desc, loc)
213  {}
214 
216  ~MemoryAllocationError() noexcept override;
217 
218  const char *
219  GetNameOfClass() const override
220  {
221  return "MemoryAllocationError";
222  }
223 };
224 
230 class ITKCommon_EXPORT RangeError : public ExceptionObject
231 {
232 public:
235  RangeError() noexcept
236  : ExceptionObject()
237  {}
238 
240  RangeError(const char * file, unsigned int lineNumber)
241  : ExceptionObject(file, lineNumber)
242  {}
243 
245  RangeError(const std::string & file, unsigned int lineNumber)
246  : ExceptionObject(file, lineNumber)
247  {}
248 
250  ~RangeError() noexcept override;
251 
252  const char *
253  GetNameOfClass() const override
254  {
255  return "RangeError";
256  }
257 };
258 
265 class ITKCommon_EXPORT InvalidArgumentError : public ExceptionObject
266 {
267 public:
272  InvalidArgumentError() noexcept
273  : ExceptionObject()
274  {}
275 
279  InvalidArgumentError(const char * file, unsigned int lineNumber)
280  : ExceptionObject(file, lineNumber)
281  {}
282 
286  InvalidArgumentError(const std::string & file, unsigned int lineNumber)
287  : ExceptionObject(file, lineNumber)
288  {}
289 
291  ~InvalidArgumentError() noexcept override;
292 
293  const char *
294  GetNameOfClass() const override
295  {
296  return "InvalidArgumentError";
297  }
298 };
299 
305 class ITKCommon_EXPORT IncompatibleOperandsError : public ExceptionObject
306 {
307 public:
310  IncompatibleOperandsError() noexcept
311  : ExceptionObject()
312  {}
313 
315  IncompatibleOperandsError(const char * file, unsigned int lineNumber)
316  : ExceptionObject(file, lineNumber)
317  {}
318 
320  IncompatibleOperandsError(const std::string & file, unsigned int lineNumber)
321  : ExceptionObject(file, lineNumber)
322  {}
323 
325  ~IncompatibleOperandsError() noexcept override;
326 
327  const char *
328  GetNameOfClass() const override
329  {
330  return "IncompatibleOperandsError";
331  }
332 };
333 
339 class ITKCommon_EXPORT ProcessAborted : public ExceptionObject
340 {
341 public:
344  ProcessAborted()
345  : ExceptionObject()
346  {
347  this->SetDescription("Filter execution was aborted by an external request");
348  }
349 
351  ProcessAborted(const char * file, unsigned int lineNumber)
352  : ExceptionObject(file, lineNumber)
353  {
354  this->SetDescription("Filter execution was aborted by an external request");
355  }
356 
358  ProcessAborted(const std::string & file, unsigned int lineNumber)
359  : ExceptionObject(file, lineNumber)
360  {
361  this->SetDescription("Filter execution was aborted by an external request");
362  }
363 
365  ~ProcessAborted() noexcept override;
366 
367  const char *
368  GetNameOfClass() const override
369  {
370  return "ProcessAborted";
371  }
372 };
373 } // end namespace itk
374 
375 #endif // itkExceptionObject_h
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:213
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itkSmartPointer.h
itk::Math::e
static constexpr double e
Definition: itkMath.h:54