exceptions.hh

Go to the documentation of this file.
1 
11 #ifndef exceptions_hh
12 #define exceptions_hh
13 
14 #include <exception>
15 #include <iostream>
16 #include <string>
17 #include <cstdlib>
18 #include <sstream>
19 #include <new>
20 
21 #include "outputOperator.hh"
22 
23 namespace concepts {
24 
25  // ************************************************************ Stacktrace **
26 
34  class Stacktrace {
35  public:
37  static void print();
39  static bool& doit() { return doit_; }
40  private:
41  static bool doit_;
42  };
43 
44  // ********************************************************* ExceptionBase **
45 
84  class ExceptionBase : public std::exception,
85  public OutputOperator
86  {
87  public:
89  ExceptionBase() throw();
90 
91  ExceptionBase(const std::string& message) throw()
92  {
93  new(this) ExceptionBase("(undefined)", -1, "(undefined)", message);
94  }
95 
103  ExceptionBase(const std::string& file, const unsigned int line,
104  const std::string& function, const std::string& excName) throw();
105 
106  virtual ~ExceptionBase() throw();
107 
115  void setFields(const std::string& file, const unsigned int line,
116  const std::string& function,
117  const std::string& excName) throw();
118 
119  protected:
120  virtual std::ostream& info(std::ostream& os) const throw();
121 
123  std::string file_;
124 
126  unsigned int line_;
127 
129  std::string function_;
130 
132  std::string excName_;
133 
134 
135  };
136 
137  // ***************************************************** DimensionMismatch **
138 
143  class DimensionMismatch : public ExceptionBase {};
144 
145 
146  // ***************************************************** WrongInputException **
147 
153 
154  public:
155 
156  WrongInputException(const std::string& disc) throw();
157 
166  WrongInputException(const std::string& file, const unsigned int line,
167  const std::string& function, const std::string& disc = std::string("")) throw();
168 
170  virtual const char* what() const throw();
171 
172  virtual ~WrongInputException() throw();
173  protected:
174  virtual std::ostream& info(std::ostream& os) const throw();
175 
176  // output Message that is alife in the throw process calling what() routine
177  mutable std::string outputMessage_ ;
178 
179  std::string disc_ ;
180 
181 
182  };
183 
184  // ****************************************************** IndexNotExisting **
185 
191  class IndexNotExisting : public ExceptionBase {};
192 
193  // ******************************************************** MissingFeature **
194 
206  class MissingFeature : public ExceptionBase {
207  public:
212  MissingFeature(const std::string& feature) throw();
213 
222  MissingFeature(const std::string& file, const unsigned int line,
223  const std::string& function, const std::string& excName,
224  const std::string& feature = std::string("")) throw();
225 
226  virtual ~MissingFeature() throw();
227  protected:
228  virtual std::ostream& info(std::ostream& os) const throw();
229 
231  std::string feature_;
232  };
233 
234  // ************************************************************* Assertion **
235 
258  class Assertion : public MissingFeature {
259  public:
261  Assertion() throw();
262 
271  Assertion(const std::string& file, const unsigned int line,
272  const std::string& function, const std::string& excName,
273  const std::string& cond) throw();
274 
275  virtual ~Assertion() throw();
276 
285  void setFieldsAssert(const std::string& file, const unsigned int line,
286  const std::string& function,
287  const std::string& excName,
288  const std::string& cond = std::string("")) throw();
289  protected:
290  virtual std::ostream& info(std::ostream& os) const throw();
291  };
292 
293  // ************************************************** exception_set_fields **
294 
295 #ifndef __GNUC__
296 # define __PRETTY_FUNCTION__ "(unknown)"
297 #endif
298 
315  template<class exc>
316  exc exception_set_fields(exc e, const std::string& file,
317  const unsigned int line,
318  const std::string& function,
319  const std::string& excName) {
320  e.setFields(file, line, function, excName);
321 #ifdef DEBUG
323 #endif
324 #ifdef __SUNPRO_CC
325  std::cout << e << std::endl;
326  std::exit(0);
327 #endif
328  return e;
329  }
330 
331  // ***************************************************** conceptsException **
332 
344 #define conceptsException(exc) \
345  exception_set_fields(exc, std::string(__FILE__), __LINE__, \
346  std::string(__PRETTY_FUNCTION__), std::string(#exc))
347 
348 #define CONCEPTS_SIMPLE_EXC(msg) \
349  exception_set_fields(ExceptionBase(), std::string(__FILE__), __LINE__, \
350  std::string(__PRETTY_FUNCTION__), std::string(msg))
351 
352  // ************************************************ exception_throw_assert **
353 
363  template<class exc>
364  void exception_throw_assert(const std::string& file, int line,
365  const std::string& function,
366  const std::string& exc_name,
367  const std::string& cond, exc e) {
368  e.setFieldsAssert(file, line, function, exc_name, cond);
370 #ifdef __SUNPRO_CC
371  std::cout << e << std::endl;
372  std::exit(0);
373 #else
374  throw e;
375 #endif
376  }
377 
378  // ******************************************************** conceptsAssert **
379 
380 #ifndef DEBUG
381 
394 #define conceptsAssert(cond, exc) { }
395 #else
396 #define conceptsAssert(cond, exc) { \
397  if (!(cond)) { \
398  std::stringstream errorMsg; \
399  errorMsg << "by violating condition <" << #cond << ">"; \
400  exception_throw_assert(std::string(__FILE__), __LINE__, \
401  std::string(__PRETTY_FUNCTION__), \
402  std::string(#exc), errorMsg.str(), exc); \
403  } \
404  }
405 #endif // DEBUG
406 
411 #define conceptsThrowSimpleE(msg) { \
412  std::stringstream errorMsg; \
413  errorMsg << msg; \
414  exception_throw_assert(std::string(__FILE__), __LINE__, \
415  std::string(__PRETTY_FUNCTION__), \
416  std::string("SimpleException"), errorMsg.str(), concepts::Assertion()); \
417  }
418 
419 
420  // ******************************************************* conceptsAssert3 **
421 
422 #ifndef DEBUG
423 
442 #define conceptsAssert3(cond, exc, msg) { }
443 #else
444 #define conceptsAssert3(cond, exc, msg) { \
445  if (!(cond)) { \
446  std::stringstream errorMsg; \
447  errorMsg << "by violating condition <" << #cond << ">, " << msg; \
448  exception_throw_assert(std::string(__FILE__), __LINE__, \
449  std::string(__PRETTY_FUNCTION__), \
450  std::string(#exc), errorMsg.str(), exc); \
451  } \
452  }
453 #endif // DEBUG
454 
455 } // namespace
456 
457 #endif // exceptions_hh
MissingFeature(const std::string &file, const unsigned int line, const std::string &function, const std::string &excName, const std::string &feature=std::string(""))
Constructor.
ExceptionBase()
Default constructor.
Exception class to express dimensions not matching.
Definition: exceptions.hh:143
Base class for exceptions.
Definition: exceptions.hh:86
void exception_throw_assert(const std::string &file, int line, const std::string &function, const std::string &exc_name, const std::string &cond, exc e)
Sets the fields of an assertion and throws it.
Definition: exceptions.hh:364
static bool & doit()
If doit is set to false, no stacktrace is printed.
Definition: exceptions.hh:39
unsigned int line_
Line where the exception was thrown from.
Definition: exceptions.hh:126
static void print()
Prints the stacktrace.
static bool doit_
Definition: exceptions.hh:41
std::string function_
Name of the function that threw the exception.
Definition: exceptions.hh:129
WrongInputException(const std::string &disc)
Assertion()
Default constructor.
Exception class for assertions.
Definition: exceptions.hh:258
std::string file_
Filename where the exception was thrown from.
Definition: exceptions.hh:123
WrongInputException(const std::string &file, const unsigned int line, const std::string &function, const std::string &disc=std::string(""))
Constructor.
Exception class to express an input criteria was not met.
Definition: exceptions.hh:152
ExceptionBase(const std::string &file, const unsigned int line, const std::string &function, const std::string &excName)
Constructor.
void setFields(const std::string &file, const unsigned int line, const std::string &function, const std::string &excName)
Sets the fields of this exception.
Exception class to express that an index in a dynamic array does not exist.
Definition: exceptions.hh:191
Exception class to express a missing feature.
Definition: exceptions.hh:206
Dumps a stack trace using gdb.
Definition: exceptions.hh:34
exc exception_set_fields(exc e, const std::string &file, const unsigned int line, const std::string &function, const std::string &excName)
Sets fields on exception and throws it.
Definition: exceptions.hh:316
MissingFeature(const std::string &feature)
Constructor.
std::string excName_
The name of the exception with calling sequence.
Definition: exceptions.hh:132
Class providing an output operator.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich