CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkException.h
Go to the documentation of this file.
1 /*=============================================================================
2 
3  Library: CTK
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 =============================================================================*/
21 
22 
23 #ifndef __ctkException_h
24 #define __ctkException_h
25 
26 // Qt includes
27 #include <QString>
28 #include <QSet>
29 
30 // CTK includes
31 #include <ctkCoreExport.h>
32 #include <ctkBackTrace.h>
33 
34 //---------------------------------------------------------------------------
45 class CTK_CORE_EXPORT ctkException : public std::exception, public ctkBackTrace
46 {
47 public:
48 
50  {
51  public:
52 
54 
55  QDebug print(QDebug dbg) const;
56 
57  private:
58 
59  const ctkException* Exc;
60  };
61 
66  explicit ctkException(const QString& msg);
67 
73  ctkException(const QString& msg, const ctkException& cause);
74 
80 
87 
88  ~ctkException() throw();
89 
94  const ctkException* cause() const throw();
95 
100  void setCause(const ctkException& cause);
101 
106  virtual const char* name() const throw();
107 
112  virtual const char* className() const throw();
113 
118  virtual const char* what() const throw();
119 
124  QString message() const throw();
125 
138  TraceManipulator printStackTrace() const;
139 
145  virtual ctkException* clone() const;
146 
150  virtual void rethrow() const;
151 
152 protected:
153 
154  friend class TraceManipulator;
155 
161  virtual QDebug printStackTrace(QDebug dbg) const;
162 
163 private:
164 
165  QString Msg;
166  mutable std::string WhatMsg;
167  ctkException* NestedException;
168 
169  void printEnclosedStackTrace(QDebug dbg, const QList<QString>& enclosingTrace,
170  const QString& caption, const QString& prefix,
171  QSet<const ctkException*>& dejaVu);
172 
173 };
174 
175 //---------------------------------------------------------------------------
179 CTK_CORE_EXPORT QDebug operator<<(QDebug dbg, const ctkException& exc);
180 
181 //---------------------------------------------------------------------------
185 CTK_CORE_EXPORT QDebug operator<<(QDebug dbg, const ctkException::TraceManipulator& trace);
186 
187 //---------------------------------------------------------------------------
196 #define CTK_DECLARE_EXCEPTION(API, CLS, BASE) \
197  class API CLS : public BASE \
198  { \
199  public: \
200  explicit CLS(const QString& msg); \
201  CLS(const QString& msg, const ctkException& exc); \
202  CLS(const CLS& exc); \
203  ~CLS() throw(); \
204  CLS& operator = (const CLS& exc); \
205  const char* name() const throw(); \
206  CLS* clone() const; \
207  void rethrow() const ; \
208  };
209 
210 //---------------------------------------------------------------------------
219 #define CTK_IMPLEMENT_EXCEPTION(CLS, BASE, NAME) \
220  CLS::CLS(const QString& msg) : BASE(msg) \
221  { } \
222  CLS::CLS(const QString& msg, const ctkException& exc) : BASE(msg, exc) \
223  { } \
224  CLS::CLS(const CLS& exc) : BASE(exc) \
225  { } \
226  CLS::~CLS() throw() \
227  { } \
228  CLS& CLS::operator = (const CLS& exc) \
229  { \
230  BASE::operator = (exc); \
231  return *this; \
232  } \
233  const char* CLS::name() const throw() \
234  { \
235  return NAME; \
236  } \
237  CLS* CLS::clone() const \
238  { \
239  return new CLS(*this); \
240  } \
241  void CLS::rethrow() const \
242  { \
243  throw *this; \
244  }
245 
250 
251 #endif // __ctkException_h
Obtains a back trace from the current execution context.
Definition: ctkBackTrace.h:44
TraceManipulator(const ctkException *e)
QDebug print(QDebug dbg) const
The base class for all exceptions defined in CTK.
Definition: ctkException.h:46
ctkException(const QString &msg, const ctkException &cause)
Create a new ctkException containing another exception as the cause.
ctkException(const QString &msg)
Create a new ctkException.
ctkException(const ctkException &o)
Copy constructor.
ctkException & operator=(const ctkException &o)
Assignment operator.
#define CTK_DECLARE_EXCEPTION(API, CLS, BASE)
Quickly declare a ctkException sub-class.
Definition: ctkException.h:196