16 #ifndef dealii__exceptions_h
17 #define dealii__exceptions_h
19 #include <deal.II/base/config.h>
25 DEAL_II_NAMESPACE_OPEN
69 const char *exc_name);
75 virtual const char *
what()
const DEAL_II_NOEXCEPT;
91 virtual void print_info (std::ostream &out)
const;
113 const char *
function;
137 #ifdef DEAL_II_HAVE_GLIBC_STACKTRACE
170 namespace deal_II_exceptions
274 const char *
function,
276 const char *exc_name,
280 e.set_fields (file, line,
function, cond, exc_name);
285 ::deal_II_exceptions::internals::abort(e);
288 ::deal_II_exceptions::internals::abort(e,
true);
313 #define Assert(cond, exc) \
316 ::deal_II_exceptions::internals:: \
317 issue_error(::deal_II_exceptions::internals::abort_on_exception,\
318 __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
321 #define Assert(cond, exc) \
342 #define AssertNothrow(cond, exc) \
345 ::deal_II_exceptions::internals:: \
347 ::deal_II_exceptions::internals::abort_nothrow_on_exception, \
348 __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
351 #define AssertNothrow(cond, exc) \
368 #ifdef DEAL_II_HAVE_BUILTIN_EXPECT
369 #define AssertThrow(cond, exc) \
371 if (__builtin_expect(!(cond), false)) \
372 ::deal_II_exceptions::internals:: \
373 issue_error(::deal_II_exceptions::internals::throw_on_exception,\
374 __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
377 #define AssertThrow(cond, exc) \
380 ::deal_II_exceptions::internals:: \
381 issue_error(::deal_II_exceptions::internals::throw_on_exception,\
382 __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
396 #define DeclException0(Exception0) \
397 class Exception0 : public ::ExceptionBase {}
408 #define DeclExceptionMsg(Exception, defaulttext) \
409 class Exception : public ::ExceptionBase \
412 Exception (const std::string &msg = defaulttext) : arg (msg) {} \
413 virtual ~Exception () DEAL_II_NOEXCEPT {} \
414 virtual void print_info (std::ostream &out) const { \
415 out << " " << arg << std::endl; \
418 const std::string arg; \
427 #define DeclException1(Exception1, type1, outsequence) \
428 class Exception1 : public ::ExceptionBase { \
430 Exception1 (const type1 a1) : arg1 (a1) {} \
431 virtual ~Exception1 () DEAL_II_NOEXCEPT {} \
432 virtual void print_info (std::ostream &out) const { \
433 out << " " outsequence << std::endl; \
446 #define DeclException2(Exception2, type1, type2, outsequence) \
447 class Exception2 : public ::ExceptionBase { \
449 Exception2 (const type1 a1, const type2 a2) : \
450 arg1 (a1), arg2(a2) {} \
451 virtual ~Exception2 () DEAL_II_NOEXCEPT {} \
452 virtual void print_info (std::ostream &out) const { \
453 out << " " outsequence << std::endl; \
467 #define DeclException3(Exception3, type1, type2, type3, outsequence) \
468 class Exception3 : public ::ExceptionBase { \
470 Exception3 (const type1 a1, const type2 a2, const type3 a3) : \
471 arg1 (a1), arg2(a2), arg3(a3) {} \
472 virtual ~Exception3 () DEAL_II_NOEXCEPT {} \
473 virtual void print_info (std::ostream &out) const { \
474 out << " " outsequence << std::endl; \
489 #define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
490 class Exception4 : public ::ExceptionBase { \
492 Exception4 (const type1 a1, const type2 a2, \
493 const type3 a3, const type4 a4) : \
494 arg1 (a1), arg2(a2), arg3(a3), arg4(a4) {} \
495 virtual ~Exception4 () DEAL_II_NOEXCEPT {} \
496 virtual void print_info (std::ostream &out) const { \
497 out << " " outsequence << std::endl; \
513 #define DeclException5(Exception5, type1, type2, type3, type4, type5, outsequence) \
514 class Exception5 : public ::ExceptionBase { \
516 Exception5 (const type1 a1, const type2 a2, const type3 a3, \
517 const type4 a4, const type5 a5) : \
518 arg1 (a1), arg2(a2), arg3(a3), arg4(a4), arg5(a5) {} \
519 virtual ~Exception5 () DEAL_II_NOEXCEPT {} \
520 virtual void print_info (std::ostream &out) const { \
521 out << " " outsequence << std::endl; \
541 #define DeclException0(Exception0) \
543 static ::ExceptionBase& Exception0 ()
553 #define DeclExceptionMsg(Exception, defaulttext) \
556 static ::ExceptionBase& Exception ()
564 #define DeclException1(Exception1, type1, outsequence) \
567 static ::ExceptionBase& Exception1 (type1 arg1)
576 #define DeclException2(Exception2, type1, type2, outsequence) \
579 static ::ExceptionBase& Exception2 (type1 arg1, type2 arg2)
588 #define DeclException3(Exception3, type1, type2, type3, outsequence) \
591 static ::ExceptionBase& Exception3 (type1 arg1, type2 arg2, type3 arg3)
600 #define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
603 static ::ExceptionBase& Exception4 (type1 arg1, type2 arg2, type3 arg3, type4 arg4)
612 #define DeclException5(Exception5, type1, type2, type3, type4, type5, outsequence) \
615 static ::ExceptionBase& Exception5 (type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)
631 namespace StandardExceptions
642 "A piece of code is attempting a division by zero. This is "
643 "likely going to lead to results that make no sense.");
656 std::complex<double>,
657 <<
"In a significant number of places, deal.II checks that some intermediate "
658 <<
"value is a finite number (as opposed to plus or minus infinity, or "
659 <<
"NaN/Not a Number). In the current function, we encountered a number "
660 <<
"that is not finite (its value is " << arg1 <<
" and therefore "
661 <<
"violates the current assertion).\n\n"
662 <<
"This may be due to the fact that some operation in this function "
663 <<
"created such a value, or because one of the arguments you passed "
664 <<
"to the function already had this value from some previous "
665 <<
"operation. In the latter case, this function only triggered the "
666 <<
"error but may not actually be responsible for the computation of "
667 <<
"the number that is not finite.\n\n"
668 <<
"There are two common cases where this situation happens. First, your "
669 <<
"code (or something in deal.II) divides by zero in a place where this "
670 <<
"should not happen. Or, you are trying to solve a linear system "
671 <<
"with an unsuitable solver (such as an indefinite or non-symmetric "
672 <<
"linear system using a Conjugate Gradient solver); such attempts "
673 <<
"oftentimes yield an operation somewhere that tries to divide "
674 <<
"by zero or take the square root of a negative value.\n\n"
675 <<
"In any case, when trying to find the source of the error, "
676 <<
"recall that the location where you are getting this error is "
677 <<
"simply the first place in the program where there is a check "
678 <<
"that a number (e.g., an element of a solution vector) is in fact "
679 <<
"finite, but that the actual error that computed the number "
680 <<
"may have happened far earlier. To find this location, you "
681 <<
"may want to add checks for finiteness in places of your "
682 <<
"program visited before the place where this error is produced."
683 <<
"One way to check for finiteness is to use the 'AssertIsFinite' "
690 "Your program tried to allocate some memory but this "
691 "allocation failed. Typically, this either means that "
692 "you simply do not have enough memory in your system, "
693 "or that you are (erroneously) trying to allocate "
694 "a chunk of memory that is simply beyond all reasonable "
695 "size, for example because the size of the object has "
696 "been computed incorrectly.");
703 <<
"Destroying memory handler while " << arg1
704 <<
" objects are still allocated.");
710 "An input/output error has occurred. There are a number of "
711 "reasons why this may be happening, both for reading and "
712 "writing operations."
714 "If this happens during an operation that tries to read "
715 "data: First, you may be "
716 "trying to read from a file that doesn't exist or that is "
717 "not readable given its file permissions. Second, deal.II "
718 "uses this error at times if it tries to "
719 "read information from a file but where the information "
720 "in the file does not correspond to the expected format. "
721 "An example would be a truncated file, or a mesh file "
722 "that contains not only sections that describe the "
723 "vertices and cells, but also sections for additional "
724 "data that deal.II does not understand."
726 "If this happens during an operation that tries to write "
727 "data: you may be trying to write to a file to which file "
728 "or directory permissions do not allow you to write. A "
729 "typical example is where you specify an output file in "
730 "a directory that does not exist.");
740 <<
"Could not open file " << arg1 <<
".");
751 "You are trying to use functionality in deal.II that is "
752 "currently not implemented. In many cases, this indicates "
753 "that there simply didn't appear much of a need for it, or "
754 "that the author of the original code did not have the "
755 "time to implement a particular case. If you hit this "
756 "exception, it is therefore worth the time to look into "
757 "the code to find out whether you may be able to "
758 "implement the missing functionality. If you do, please "
759 "consider providing a patch to the deal.II development "
760 "sources (see the deal.II website on how to contribute).");
783 "This exception -- which is used in many places in the "
784 "library -- usually indicates that some condition which "
785 "the author of the code thought must be satisfied at a "
786 "certain point in an algorithm, is not fulfilled. An "
787 "example would be that the first part of an algorithm "
788 "sorts elements of an array in ascending order, and "
789 "a second part of the algorithm later encounters an "
790 "element that is not larger than the previous one."
792 "There is usually not very much you can do if you "
793 "encounter such an exception since it indicates an error "
794 "in deal.II, not in your own program. Try to come up with "
795 "the smallest possible program that still demonstrates "
796 "the error and contact the deal.II mailing lists with it "
806 "You (or a place in the library) are trying to call a "
807 "function that is declared as a virtual function in a "
808 "base class but that has not been overridden in your "
811 "This exception happens in cases where the base class "
812 "cannot provide a useful default implementation for "
813 "the virtual function, but where we also do not want "
814 "to mark the function as abstract (i.e., with '=0' at the end) "
815 "because the function is not essential to the class in many "
816 "contexts. In cases like this, the base class provides "
817 "a dummy implementation that makes the compiler happy, but "
818 "that then throws the current exception."
820 "A concrete example would be the 'Function' class. It declares "
821 "the existence of 'value()' and 'gradient()' member functions, "
822 "and both are marked as 'virtual'. Derived classes have to "
823 "override these functions for the values and gradients of a "
824 "particular function. On the other hand, not every function "
825 "has a gradient, and even for those that do, not every program "
826 "actually needs to evaluate it. Consequently, there is no "
827 "*requirement* that a derived class actually override the "
828 "'gradient()' function (as there would be had it been marked "
829 "as abstract). But, since the base class cannot know how to "
830 "compute the gradient, if a derived class does not override "
831 "the 'gradient()' function and it is called anyway, then the "
832 "default implementation in the base class will simply throw "
835 "The exception you see is what happens in cases such as the "
836 "one just illustrated. To fix the problem, you need to "
837 "investigate whether the function being called should indeed have "
838 "been called; if the answer is 'yes', then you need to "
839 "implement the missing override in your class.");
859 <<
"You are trying to execute functionality that is "
860 <<
"impossible in " << arg1
861 <<
"d or simply does not make any sense.");
872 <<
"You are trying to execute functionality that is "
873 <<
"impossible in dimensions <" << arg1 <<
"," << arg2
874 <<
"> or simply does not make any sense.");
881 "In a check in the code, deal.II encountered a zero in "
882 "a place where this does not make sense. See the condition "
883 "that was being checked and that is printed further up "
884 "in the error message to get more information on what "
885 "the erroneous zero corresponds to.");
892 "The object you are trying to access is empty but it makes "
893 "no sense to attempt the operation you are trying on an "
904 std::size_t, std::size_t,
905 <<
"Dimension " << arg1 <<
" not equal to " << arg2 <<
".");
913 <<
"Dimension " << arg1 <<
" neither equal to " << arg2
914 <<
" nor to " << arg3 <<
".");
930 <<
"Index " << arg1 <<
" is not in the half-open range [" << arg2 <<
","
933 " In the current case, this half-open range is in fact empty, "
934 "suggesting that you are accessing an element of an empty "
935 "collection such as a vector that has not been set to the "
955 template <
typename T>
958 <<
"Index " << arg1 <<
" is not in the half-open range [" << arg2 <<
","
961 " In the current case, this half-open range is in fact empty, "
962 "suggesting that you are accessing an element of an empty "
963 "collection such as a vector that has not been set to the "
973 <<
"Number " << arg1 <<
" must be larger than or equal "
979 template <
typename T>
982 <<
"Number " << arg1 <<
" must be larger than or equal "
991 <<
"Division " << arg1 <<
" by " << arg2
992 <<
" has remainder different from zero.");
1003 "You are trying to use an iterator, but the iterator is "
1004 "in an invalid state. This may indicate that the iterator "
1005 "object has not been initialized, or that it has been "
1006 "moved beyond the end of the range of valid elements.");
1013 "You are trying to use an iterator, but the iterator is "
1014 "pointing past the end of the range of valid elements. "
1015 "It is not valid to dereference the iterator in this "
1039 "You are trying an operation on a vector that is only "
1040 "allowed if the vector has no ghost elements, but the "
1041 "vector you are operating on does have ghost elements. "
1042 "Specifically, vectors with ghost elements are read-only "
1043 "and cannot appear in operations that write into these "
1046 "See the glossary entry on 'Ghosted vectors' for more "
1057 "You are trying an operation of the form 'vector=s' with "
1058 "a nonzero scalar value 's'. However, such assignments "
1059 "are only allowed if the right hand side is zero.");
1065 "You are attempting to use functionality that is only available "
1066 "if deal.II was configured to use LAPACK, but cmake did not "
1067 "find a valid LAPACK library.");
1073 "You are attempting to use functionality that is only available "
1074 "if deal.II was configured to use NetCDF, but cmake did not "
1075 "find a valid NetCDF library.");
1081 "You are attempting to use functionality that is only available "
1082 "if deal.II was configured to use the function parser which "
1083 "relies on the muparser library, but cmake did not "
1084 "find a valid muparser library on your system and also did "
1085 "not choose the one that comes bundled with deal.II.");
1087 #ifdef DEAL_II_WITH_CUDA
1100 #ifdef DEAL_II_WITH_MPI
1126 ExcMPI (
const int error_code);
1128 virtual void print_info (std::ostream &out)
const;
1130 const int error_code;
1132 #endif // DEAL_II_WITH_MPI
1146 #define AssertDimension(dim1,dim2) Assert((dim1) == (dim2), \
1147 ::ExcDimensionMismatch((dim1),(dim2)))
1157 #define AssertVectorVectorDimension(vec,dim1,dim2) AssertDimension((vec).size(), (dim1)) \
1158 for (unsigned int i=0;i<dim1;++i) { AssertDimension((vec)[i].size(), (dim2)); }
1170 #define AssertIndexRange(index,range) Assert((index) < (range), \
1171 ::ExcIndexRange((index),0,(range)))
1185 #define AssertGlobalIndexRange(index,range) Assert((index) < (range), \
1186 ::ExcIndexRange<types::global_dof_index>((index),0,(range)))
1197 #define AssertIsFinite(number) Assert(::numbers::is_finite(number), \
1198 ::ExcNumberNotFinite(std::complex<double>(number)))
1200 #ifdef DEAL_II_WITH_MPI
1211 #define AssertThrowMPI(error_code) AssertThrow(error_code == MPI_SUCCESS, \
1212 ::ExcMPI(error_code))
1214 #define AssertThrowMPI(error_code) {}
1215 #endif // DEAL_II_WITH_MPI
1217 #ifdef DEAL_II_WITH_CUDA
1227 #define AssertCuda(error_code) AssertThrow(error_code == cudaSuccess, \
1228 ::ExcCudaError(cudaGetErrorString(error_code)))
1231 using namespace StandardExceptions;
1233 DEAL_II_NAMESPACE_CLOSE
static::ExceptionBase & ExcPureFunctionCalled()
static::ExceptionBase & ExcNeedsFunctionparser()
void print_exc_data(std::ostream &out) const
#define DeclException2(Exception2, type1, type2, outsequence)
static::ExceptionBase & ExcFileNotOpen(char *arg1)
static::ExceptionBase & ExcImpossibleInDimSpacedim(int arg1, int arg2)
void suppress_stacktrace_in_exceptions()
static::ExceptionBase & ExcIO()
static::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
static::ExceptionBase & ExcDimensionMismatch2(int arg1, int arg2, int arg3)
void set_additional_assert_output(const char *const p)
static::ExceptionBase & ExcNotInitialized()
static::ExceptionBase & ExcOutOfMemory()
ExceptionBase operator=(const ExceptionBase &exc)
static::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
static::ExceptionBase & ExcDivideByZero()
void * raw_stacktrace[25]
static::ExceptionBase & ExcInvalidIterator()
static::ExceptionBase & ExcNeedsLAPACK()
static::ExceptionBase & ExcInvalidState()
static::ExceptionBase & ExcMessage(std::string arg1)
void abort(const ExceptionBase &exc, bool nothrow=false)
static::ExceptionBase & ExcImpossibleInDim(int arg1)
static::ExceptionBase & ExcNeedsNetCDF()
#define DeclException1(Exception1, type1, outsequence)
static::ExceptionBase & ExcMemoryLeak(int arg1)
void set_fields(const char *file, const int line, const char *function, const char *cond, const char *exc_name)
static::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void generate_message() const
virtual void print_info(std::ostream &out) const
#define DeclExceptionMsg(Exception, defaulttext)
#define DeclException0(Exception0)
virtual ~ExceptionBase() DEAL_II_NOEXCEPT
static::ExceptionBase & ExcIndexRangeType(T arg1, T arg2, T arg3)
virtual void print_info(std::ostream &out) const
void print_stack_trace(std::ostream &out) const
static::ExceptionBase & ExcLowerRange(int arg1, int arg2)
static::ExceptionBase & ExcGhostsPresent()
static::ExceptionBase & ExcIteratorPastEnd()
static::ExceptionBase & ExcNumberNotFinite(std::complex< double > arg1)
static::ExceptionBase & ExcEmptyObject()
static::ExceptionBase & ExcNotMultiple(int arg1, int arg2)
static::ExceptionBase & ExcNotImplemented()
#define DeclException3(Exception3, type1, type2, type3, outsequence)
const char * get_exc_name() const
void disable_abort_on_exception()
static::ExceptionBase & ExcZero()
virtual const char * what() const DEAL_II_NOEXCEPT
void issue_error(ExceptionHandling handling, const char *file, int line, const char *function, const char *cond, const char *exc_name, exc e)
static::ExceptionBase & ExcLowerRangeType(T arg1, T arg2)
static::ExceptionBase & ExcInternalError()