Reference documentation for deal.II version 8.1.0
exceptions.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 // @f$Id: exceptions.h 31932 2013-12-08 02:15:54Z heister @f$
3 //
4 // Copyright (C) 1998 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__exceptions_h
18 #define __deal2__exceptions_h
19 
25 #include <deal.II/base/config.h>
26 
27 #include <exception>
28 #include <string>
29 #include <ostream>
30 
32 
33 
46 class ExceptionBase : public std::exception
47 {
48 public:
52  ExceptionBase ();
53 
58 
62  virtual ~ExceptionBase () throw();
63 
69  void set_fields (const char *file,
70  const int line,
71  const char *function,
72  const char *cond,
73  const char *exc_name);
74 
75 
79  virtual const char *what() const throw();
80 
84  const char *get_exc_name() const;
85 
89  void print_exc_data (std::ostream &out) const;
90 
95  virtual void print_info (std::ostream &out) const;
96 
101  void print_stack_trace (std::ostream &out) const;
102 
103 protected:
107  const char *file;
108 
112  unsigned int line;
113 
117  const char *function;
118 
122  const char *cond;
123 
127  const char *exc;
128 
133  mutable char **stacktrace;
134 
140 
141 #ifdef HAVE_GLIBC_STACKTRACE
142 
145  void *raw_stacktrace[25];
146 #endif
147 
148 private:
152  void generate_message() const;
153 
158  mutable std::string what_str;
159 };
160 
161 
162 
170 {
171 
189  void set_additional_assert_output (const char *const p);
190 
200 
213 
222  namespace internals
223  {
224 
239  void abort (const ExceptionBase &exc, bool nothrow = false);
240 
245  {
246  abort_on_exception,
247  throw_on_exception,
248  abort_nothrow_on_exception
249  };
250 
257  template <class exc>
259  const char *file,
260  int line,
261  const char *function,
262  const char *cond,
263  const char *exc_name,
264  exc e)
265  {
266  // Fill the fields of the exception object
267  e.set_fields (file, line, function, cond, exc_name);
268 
269  switch (handling)
270  {
271  case abort_on_exception:
273  break;
274  case abort_nothrow_on_exception:
275  ::deal_II_exceptions::internals::abort(e, /*nothrow =*/ true);
276  break;
277  case throw_on_exception:
278  throw e;
279  }
280  }
281 
282  } /*namespace internals*/
283 
284 } /*namespace deal_II_exceptions*/
285 
286 
287 
298 #ifdef DEBUG
299 #define Assert(cond, exc) \
300  { \
301  if (!(cond)) \
302  ::deal_II_exceptions::internals:: \
303  issue_error(::deal_II_exceptions::internals::abort_on_exception,\
304  __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
305  }
306 #else
307 #define Assert(cond, exc) \
308  {}
309 #endif
310 
311 
312 
326 #ifdef DEBUG
327 #define AssertNothrow(cond, exc) \
328  { \
329  if (!(cond)) \
330  ::deal_II_exceptions::internals:: \
331  issue_error( \
332  ::deal_II_exceptions::internals::abort_nothrow_on_exception, \
333  __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
334  }
335 #else
336 #define AssertNothrow(cond, exc) \
337  {}
338 #endif
339 
340 
341 
353 #ifdef HAVE_BUILTIN_EXPECT
354 #define AssertThrow(cond, exc) \
355  { \
356  if (__builtin_expect(!(cond), false)) \
357  ::deal_II_exceptions::internals:: \
358  issue_error(::deal_II_exceptions::internals::throw_on_exception,\
359  __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
360  }
361 #else /*ifdef HAVE_BUILTIN_EXPECT*/
362 #define AssertThrow(cond, exc) \
363  { \
364  if (!(cond)) \
365  ::deal_II_exceptions::internals:: \
366  issue_error(::deal_II_exceptions::internals::throw_on_exception,\
367  __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
368  }
369 #endif /*ifdef HAVE_BUILTIN_EXPECT*/
370 
371 
372 
373 #ifndef DOXYGEN
374 
381 #define DeclException0(Exception0) \
382  class Exception0 : public ::ExceptionBase {}
383 
384 
391 #define DeclException1(Exception1, type1, outsequence) \
392  class Exception1 : public ::ExceptionBase { \
393  public: \
394  Exception1 (const type1 a1) : arg1 (a1) {} \
395  virtual ~Exception1 () throw () {} \
396  virtual void print_info (std::ostream &out) const { \
397  out outsequence << std::endl; \
398  } \
399  private: \
400  const type1 arg1; \
401  }
402 
403 
410 #define DeclException2(Exception2, type1, type2, outsequence) \
411  class Exception2 : public ::ExceptionBase { \
412  public: \
413  Exception2 (const type1 a1, const type2 a2) : \
414  arg1 (a1), arg2(a2) {} \
415  virtual ~Exception2 () throw () {} \
416  virtual void print_info (std::ostream &out) const { \
417  out outsequence << std::endl; \
418  } \
419  private: \
420  const type1 arg1; \
421  const type2 arg2; \
422  }
423 
424 
431 #define DeclException3(Exception3, type1, type2, type3, outsequence) \
432  class Exception3 : public ::ExceptionBase { \
433  public: \
434  Exception3 (const type1 a1, const type2 a2, const type3 a3) : \
435  arg1 (a1), arg2(a2), arg3(a3) {} \
436  virtual ~Exception3 () throw () {} \
437  virtual void print_info (std::ostream &out) const { \
438  out outsequence << std::endl; \
439  } \
440  private: \
441  const type1 arg1; \
442  const type2 arg2; \
443  const type3 arg3; \
444  }
445 
446 
453 #define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
454  class Exception4 : public ::ExceptionBase { \
455  public: \
456  Exception4 (const type1 a1, const type2 a2, \
457  const type3 a3, const type4 a4) : \
458  arg1 (a1), arg2(a2), arg3(a3), arg4(a4) {} \
459  virtual ~Exception4 () throw () {} \
460  virtual void print_info (std::ostream &out) const { \
461  out outsequence << std::endl; \
462  } \
463  private: \
464  const type1 arg1; \
465  const type2 arg2; \
466  const type3 arg3; \
467  const type4 arg4; \
468  }
469 
470 
477 #define DeclException5(Exception5, type1, type2, type3, type4, type5, outsequence) \
478  class Exception5 : public ::ExceptionBase { \
479  public: \
480  Exception5 (const type1 a1, const type2 a2, const type3 a3, \
481  const type4 a4, const type5 a5) : \
482  arg1 (a1), arg2(a2), arg3(a3), arg4(a4), arg5(a5) {} \
483  virtual ~Exception5 () throw () {} \
484  virtual void print_info (std::ostream &out) const { \
485  out outsequence << std::endl; \
486  } \
487  private: \
488  const type1 arg1; \
489  const type2 arg2; \
490  const type3 arg3; \
491  const type4 arg4; \
492  const type5 arg5; \
493  }
494 
495 #else /*ifndef DOXYGEN*/
496 
497 // Dummy definitions for doxygen:
498 
505 #define DeclException0(Exception0) \
506  static ::ExceptionBase& Exception0 ()
507 
508 
515 #define DeclException1(Exception1, type1, outsequence) \
516  static ::ExceptionBase& Exception1 (type1 arg1) throw (errortext outsequence)
517 
518 
525 #define DeclException2(Exception2, type1, type2, outsequence) \
526  static ::ExceptionBase& Exception2 (type1 arg1, type2 arg2) throw (errortext outsequence)
527 
528 
535 #define DeclException3(Exception3, type1, type2, type3, outsequence) \
536  static ::ExceptionBase& Exception3 (type1 arg1, type2 arg2, type3 arg3) throw (errortext outsequence)
537 
538 
545 #define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
546  static ::ExceptionBase& Exception4 (type1 arg1, type2 arg2, type3 arg3, type4 arg4) throw (errortext outsequence)
547 
548 
555 #define DeclException5(Exception5, type1, type2, type3, type4, type5, outsequence) \
556  static ::ExceptionBase& Exception5 (type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) throw (errortext outsequence)
557 
558 #endif /*ifndef DOXYGEN*/
559 
560 
573 {
578 
588 
597 
602 
609  << "Destroying memory handler while " << arg1
610  << " objects are still allocated");
611 
616 
624  char *,
625  << "Could not open file " << arg1);
626 
636 
660 
668 
675 
680 
685 
693  int,
694  << "Impossible in " << arg1 << "d.");
695 
700 
706 
715  std::size_t, std::size_t,
716  << "Dimension " << arg1 << " not equal to " << arg2);
717 
723  int, int, int,
724  << "Dimension " << arg1 << " neither equal to " << arg2
725  << " nor to " << arg3);
726 
740  int, int, int,
741  << "Index " << arg1 << " is not in [" << arg2 << ","
742  << arg3 << "[");
743 
748  template<typename T>
750  T,T,T,
751  << "Index " << arg1 << " is not in [" << arg2 << ","
752  << arg3 << "[");
753 
758  int, int,
759  << "Number " << arg1 << " must be larger or equal "
760  << arg2);
761 
765  template<typename T>
767  T, T,
768  << "Number " << arg1 << " must be larger or equal "
769  << arg2);
770 
776  int, int,
777  << "Division " << arg1 << " by " << arg2
778  << " has remainder different from zero");
779 
790 
796 
811  std::string,
812  << arg1);
813 
818 
827 
832 
837 
842 
843 
845 } /*namespace StandardExceptions*/
846 
847 
858 #define AssertDimension(dim1,dim2) Assert((dim1) == (dim2), \
859  ExcDimensionMismatch((dim1),(dim2)))
860 
861 
870 #define AssertVectorVectorDimension(vec,dim1,dim2) AssertDimension((vec).size(), (dim1)) \
871  for (unsigned int i=0;i<dim1;++i) { AssertDimension((vec)[i].size(), (dim2)); }
872 
873 
888 #define AssertIndexRange(index,range) Assert((index) < (range), \
889  ExcIndexRange((index),0,(range)))
890 
891 #define AssertGlobalIndexRange(index,range) Assert((index) < (range), \
892  ExcIndexRange<types::global_dof_index>((index),0,(range)))
893 
894 using namespace StandardExceptions;
895 
896 DEAL_II_NAMESPACE_CLOSE
897 
898 #endif
::ExceptionBase & ExcLowerRangeType(T arg1, T arg2)
::ExceptionBase & ExcPureFunctionCalled()
void print_exc_data(std::ostream &out) const
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:525
void suppress_stacktrace_in_exceptions()
virtual ~ExceptionBase()
::ExceptionBase & ExcOutOfMemory()
::ExceptionBase & ExcIndexRangeType(T arg1, T arg2, T arg3)
::ExceptionBase & ExcMessage(std::string arg1)
void set_additional_assert_output(const char *const p)
virtual void print_info(std::ostream &out) const
::ExceptionBase & ExcLowerRange(int arg1, int arg2)
::ExceptionBase & ExcInvalidState()
::ExceptionBase & ExcIO()
const char * get_exc_name() const
void abort(const ExceptionBase &exc, bool nothrow=false)
::ExceptionBase & ExcGhostsPresent()
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:515
::ExceptionBase & ExcEmptyObject()
void set_fields(const char *file, const int line, const char *function, const char *cond, const char *exc_name)
void generate_message() const
#define DeclException0(Exception0)
Definition: exceptions.h:505
::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
::ExceptionBase & ExcInvalidConstructorCall()
::ExceptionBase & ExcNeedsFunctionparser()
::ExceptionBase & ExcNeedsNetCDF()
::ExceptionBase & ExcImpossibleInDim(int arg1)
std::string what_str
Definition: exceptions.h:158
::ExceptionBase & ExcNeedsLAPACK()
unsigned int line
Definition: exceptions.h:112
void print_stack_trace(std::ostream &out) const
const char * exc
Definition: exceptions.h:127
::ExceptionBase & ExcFileNotOpen(char *arg1)
::ExceptionBase & ExcIteratorPastEnd()
const char * cond
Definition: exceptions.h:122
::ExceptionBase & ExcNumberNotFinite()
const char * file
Definition: exceptions.h:107
::ExceptionBase & ExcNotMultiple(int arg1, int arg2)
::ExceptionBase & ExcNotImplemented()
::ExceptionBase & ExcNotInitialized()
::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
::ExceptionBase & ExcDimensionMismatch2(int arg1, int arg2, int arg3)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:535
virtual const char * what() const
::ExceptionBase & ExcInvalidIterator()
::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
char ** stacktrace
Definition: exceptions.h:133
void disable_abort_on_exception()
::ExceptionBase & ExcInternalError()
::ExceptionBase & ExcDivideByZero()
::ExceptionBase & ExcZero()
::ExceptionBase & ExcMemoryLeak(int arg1)
int n_stacktrace_frames
Definition: exceptions.h:139
void issue_error(ExceptionHandling handling, const char *file, int line, const char *function, const char *cond, const char *exc_name, exc e)
Definition: exceptions.h:258