ITK  5.1.0
Insight Toolkit
itkExceptionObject.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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  using Superclass = std::exception;
55  ExceptionObject() noexcept;
56  explicit ExceptionObject(const char * file,
57  unsigned int lineNumber = 0,
58  const char * desc = "None",
59  const char * loc = "Unknown");
60  explicit ExceptionObject(const std::string & file,
61  unsigned int lineNumber = 0,
62  const std::string & desc = "None",
63  const std::string & loc = "Unknown");
64  ExceptionObject(const ExceptionObject & orig) noexcept;
66 
68  ~ExceptionObject() noexcept override;
69 
71  ExceptionObject &
72  operator=(const ExceptionObject & orig) noexcept;
73 
75  virtual bool
76  operator==(const ExceptionObject & orig);
77 
78  virtual const char *
79  GetNameOfClass() const
80  {
81  return "ExceptionObject";
82  }
83 
88  virtual void
89  Print(std::ostream & os) const;
90 
94  virtual void
95  SetLocation(const std::string & s);
96 
97  virtual void
98  SetDescription(const std::string & s);
99 
100  virtual void
101  SetLocation(const char * s);
102 
103  virtual void
104  SetDescription(const char * s);
105 
106  virtual const char *
107  GetLocation() const;
108 
109  virtual const char *
110  GetDescription() const;
111 
113  virtual const char *
114  GetFile() const;
115 
117  virtual unsigned int
118  GetLine() const;
119 
121  const char *
122  what() const noexcept override;
123 
124 private:
150  class ReferenceCounterInterface
151  {
152  public:
153  virtual void
154  Register() const = 0;
155 
156  virtual void
157  UnRegister() const = 0;
158 
159  ReferenceCounterInterface();
160  virtual ~ReferenceCounterInterface();
161  };
162  class ExceptionData;
163  class ReferenceCountedExceptionData;
164  SmartPointer<const ReferenceCounterInterface> m_ExceptionData;
165  const ExceptionData *
166  GetExceptionData() const;
167 };
168 
170 inline std::ostream &
171 operator<<(std::ostream & os, ExceptionObject & e)
172 {
173  (&e)->Print(os);
174  return os;
175 }
177 
187 class ITKCommon_EXPORT MemoryAllocationError : public ExceptionObject
188 {
189 public:
192  MemoryAllocationError() noexcept
193  : ExceptionObject()
194  {}
195 
197  MemoryAllocationError(const char * file, unsigned int lineNumber)
198  : ExceptionObject(file, lineNumber)
199  {}
200 
202  MemoryAllocationError(const std::string & file, unsigned int lineNumber)
203  : ExceptionObject(file, lineNumber)
204  {}
205 
207  MemoryAllocationError(const std::string & file,
208  unsigned int lineNumber,
209  const std::string & desc,
210  const std::string & loc)
211  : ExceptionObject(file, lineNumber, desc, loc)
212  {}
213 
215  ~MemoryAllocationError() noexcept override;
216 
217  const char *
218  GetNameOfClass() const override
219  {
220  return "MemoryAllocationError";
221  }
222 };
223 
229 class ITKCommon_EXPORT RangeError : public ExceptionObject
230 {
231 public:
234  RangeError() noexcept
235  : ExceptionObject()
236  {}
237 
239  RangeError(const char * file, unsigned int lineNumber)
240  : ExceptionObject(file, lineNumber)
241  {}
242 
244  RangeError(const std::string & file, unsigned int lineNumber)
245  : ExceptionObject(file, lineNumber)
246  {}
247 
249  ~RangeError() noexcept override;
250 
251  const char *
252  GetNameOfClass() const override
253  {
254  return "RangeError";
255  }
256 };
257 
264 class ITKCommon_EXPORT InvalidArgumentError : public ExceptionObject
265 {
266 public:
271  InvalidArgumentError() noexcept
272  : ExceptionObject()
273  {}
274 
278  InvalidArgumentError(const char * file, unsigned int lineNumber)
279  : ExceptionObject(file, lineNumber)
280  {}
281 
285  InvalidArgumentError(const std::string & file, unsigned int lineNumber)
286  : ExceptionObject(file, lineNumber)
287  {}
288 
290  ~InvalidArgumentError() noexcept override;
291 
292  const char *
293  GetNameOfClass() const override
294  {
295  return "InvalidArgumentError";
296  }
297 };
298 
304 class ITKCommon_EXPORT IncompatibleOperandsError : public ExceptionObject
305 {
306 public:
309  IncompatibleOperandsError() noexcept
310  : ExceptionObject()
311  {}
312 
314  IncompatibleOperandsError(const char * file, unsigned int lineNumber)
315  : ExceptionObject(file, lineNumber)
316  {}
317 
319  IncompatibleOperandsError(const std::string & file, unsigned int lineNumber)
320  : ExceptionObject(file, lineNumber)
321  {}
322 
324  ~IncompatibleOperandsError() noexcept override;
325 
326  const char *
327  GetNameOfClass() const override
328  {
329  return "IncompatibleOperandsError";
330  }
331 };
332 
338 class ITKCommon_EXPORT ProcessAborted : public ExceptionObject
339 {
340 public:
343  ProcessAborted()
344  : ExceptionObject()
345  {
346  this->SetDescription("Filter execution was aborted by an external request");
347  }
348 
350  ProcessAborted(const char * file, unsigned int lineNumber)
351  : ExceptionObject(file, lineNumber)
352  {
353  this->SetDescription("Filter execution was aborted by an external request");
354  }
355 
357  ProcessAborted(const std::string & file, unsigned int lineNumber)
358  : ExceptionObject(file, lineNumber)
359  {
360  this->SetDescription("Filter execution was aborted by an external request");
361  }
362 
364  ~ProcessAborted() noexcept override;
365 
366  const char *
367  GetNameOfClass() const override
368  {
369  return "ProcessAborted";
370  }
371 };
372 } // end namespace itk
373 
374 #endif // itkExceptionObject_h
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:211
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:53