16 #ifndef dealii_exceptions_h
17 #define dealii_exceptions_h
24 #include <type_traits>
26 #ifdef DEAL_II_WITH_CUDA
27 # include <cusolverSp.h>
28 # include <cusparse.h>
83 const
char *exc_name);
90 what() const noexcept override;
150 #ifdef DEAL_II_HAVE_GLIBC_STACKTRACE
189 # define DeclException0(Exception0) \
190 class Exception0 : public ::ExceptionBase \
213 # define DeclExceptionMsg(Exception, defaulttext) \
214 class Exception : public ::ExceptionBase \
217 Exception(const std::string &msg = defaulttext) \
220 virtual ~Exception() noexcept \
223 print_info(std::ostream &out) const override \
225 out << " " << arg << std::endl; \
229 const std::string arg; \
249 # define DeclException1(Exception1, type1, outsequence) \
250 class Exception1 : public ::ExceptionBase \
253 Exception1(type1 const &a1) \
256 virtual ~Exception1() noexcept \
259 print_info(std::ostream &out) const override \
261 out << " " outsequence << std::endl; \
286 # define DeclException2(Exception2, type1, type2, outsequence) \
287 class Exception2 : public ::ExceptionBase \
290 Exception2(type1 const &a1, type2 const &a2) \
294 virtual ~Exception2() noexcept \
297 print_info(std::ostream &out) const override \
299 out << " " outsequence << std::endl; \
325 # define DeclException3(Exception3, type1, type2, type3, outsequence) \
326 class Exception3 : public ::ExceptionBase \
329 Exception3(type1 const &a1, type2 const &a2, type3 const &a3) \
334 virtual ~Exception3() noexcept \
337 print_info(std::ostream &out) const override \
339 out << " " outsequence << std::endl; \
366 # define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
367 class Exception4 : public ::ExceptionBase \
370 Exception4(type1 const &a1, \
379 virtual ~Exception4() noexcept \
382 print_info(std::ostream &out) const override \
384 out << " " outsequence << std::endl; \
412 # define DeclException5( \
413 Exception5, type1, type2, type3, type4, type5, outsequence) \
414 class Exception5 : public ::ExceptionBase \
417 Exception5(type1 const &a1, \
428 virtual ~Exception5() noexcept \
431 print_info(std::ostream &out) const override \
433 out << " " outsequence << std::endl; \
464 # define DeclException0(Exception0) \
466 static ::ExceptionBase &Exception0()
487 # define DeclExceptionMsg(Exception, defaulttext) \
490 static ::ExceptionBase &Exception()
509 # define DeclException1(Exception1, type1, outsequence) \
512 static ::ExceptionBase &Exception1(type1 arg1)
532 # define DeclException2(Exception2, type1, type2, outsequence) \
535 static ::ExceptionBase &Exception2(type1 arg1, type2 arg2)
555 # define DeclException3(Exception3, type1, type2, type3, outsequence) \
558 static ::ExceptionBase &Exception3(type1 arg1, type2 arg2, type3 arg3)
578 # define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
581 static ::ExceptionBase &Exception4(type1 arg1, \
604 # define DeclException5( \
605 Exception5, type1, type2, type3, type4, type5, outsequence) \
608 static ::ExceptionBase &Exception5( \
609 type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)
636 "A piece of code is attempting a division by zero. This is "
637 "likely going to lead to results that make no sense.");
651 std::complex<double>,
652 <<
"In a significant number of places, deal.II checks that some intermediate "
653 <<
"value is a finite number (as opposed to plus or minus infinity, or "
654 <<
"NaN/Not a Number). In the current function, we encountered a number "
655 <<
"that is not finite (its value is " << arg1 <<
" and therefore "
656 <<
"violates the current assertion).\n\n"
657 <<
"This may be due to the fact that some operation in this function "
658 <<
"created such a value, or because one of the arguments you passed "
659 <<
"to the function already had this value from some previous "
660 <<
"operation. In the latter case, this function only triggered the "
661 <<
"error but may not actually be responsible for the computation of "
662 <<
"the number that is not finite.\n\n"
663 <<
"There are two common cases where this situation happens. First, your "
664 <<
"code (or something in deal.II) divides by zero in a place where this "
665 <<
"should not happen. Or, you are trying to solve a linear system "
666 <<
"with an unsuitable solver (such as an indefinite or non-symmetric "
667 <<
"linear system using a Conjugate Gradient solver); such attempts "
668 <<
"oftentimes yield an operation somewhere that tries to divide "
669 <<
"by zero or take the square root of a negative value.\n\n"
670 <<
"In any case, when trying to find the source of the error, "
671 <<
"recall that the location where you are getting this error is "
672 <<
"simply the first place in the program where there is a check "
673 <<
"that a number (e.g., an element of a solution vector) is in fact "
674 <<
"finite, but that the actual error that computed the number "
675 <<
"may have happened far earlier. To find this location, you "
676 <<
"may want to add checks for finiteness in places of your "
677 <<
"program visited before the place where this error is produced. "
678 <<
"One way to check for finiteness is to use the 'AssertIsFinite' "
686 "Your program tried to allocate some memory but this "
687 "allocation failed. Typically, this either means that "
688 "you simply do not have enough memory in your system, "
689 "or that you are (erroneously) trying to allocate "
690 "a chunk of memory that is simply beyond all reasonable "
691 "size, for example because the size of the object has "
692 "been computed incorrectly."
694 "In the current case, the request was for "
695 << arg1 <<
" bytes.");
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
743 "If this happens during an operation that tries to read "
745 "trying to read from a file that doesn't exist or that is "
746 "not readable given its file permissions."
748 "If this happens during an operation that tries to write "
749 "data: you may be trying to write to a file to which file "
750 "or directory permissions do not allow you to write. A "
751 "typical example is where you specify an output file in "
752 "a directory that does not exist.");
763 "You are trying to use functionality in deal.II that is "
764 "currently not implemented. In many cases, this indicates "
765 "that there simply didn't appear much of a need for it, or "
766 "that the author of the original code did not have the "
767 "time to implement a particular case. If you hit this "
768 "exception, it is therefore worth the time to look into "
769 "the code to find out whether you may be able to "
770 "implement the missing functionality. If you do, please "
771 "consider providing a patch to the deal.II development "
772 "sources (see the deal.II website on how to contribute).");
795 "This exception -- which is used in many places in the "
796 "library -- usually indicates that some condition which "
797 "the author of the code thought must be satisfied at a "
798 "certain point in an algorithm, is not fulfilled. An "
799 "example would be that the first part of an algorithm "
800 "sorts elements of an array in ascending order, and "
801 "a second part of the algorithm later encounters an "
802 "element that is not larger than the previous one."
804 "There is usually not very much you can do if you "
805 "encounter such an exception since it indicates an error "
806 "in deal.II, not in your own program. Try to come up with "
807 "the smallest possible program that still demonstrates "
808 "the error and contact the deal.II mailing lists with it "
819 "You (or a place in the library) are trying to call a "
820 "function that is declared as a virtual function in a "
821 "base class but that has not been overridden in your "
824 "This exception happens in cases where the base class "
825 "cannot provide a useful default implementation for "
826 "the virtual function, but where we also do not want "
827 "to mark the function as abstract (i.e., with '=0' at the end) "
828 "because the function is not essential to the class in many "
829 "contexts. In cases like this, the base class provides "
830 "a dummy implementation that makes the compiler happy, but "
831 "that then throws the current exception."
833 "A concrete example would be the 'Function' class. It declares "
834 "the existence of 'value()' and 'gradient()' member functions, "
835 "and both are marked as 'virtual'. Derived classes have to "
836 "override these functions for the values and gradients of a "
837 "particular function. On the other hand, not every function "
838 "has a gradient, and even for those that do, not every program "
839 "actually needs to evaluate it. Consequently, there is no "
840 "*requirement* that a derived class actually override the "
841 "'gradient()' function (as there would be had it been marked "
842 "as abstract). But, since the base class cannot know how to "
843 "compute the gradient, if a derived class does not override "
844 "the 'gradient()' function and it is called anyway, then the "
845 "default implementation in the base class will simply throw "
848 "The exception you see is what happens in cases such as the "
849 "one just illustrated. To fix the problem, you need to "
850 "investigate whether the function being called should indeed have "
851 "been called; if the answer is 'yes', then you need to "
852 "implement the missing override in your class.");
859 <<
"Please provide an implementation for the function \""
880 <<
"You are trying to execute functionality that is "
881 <<
"impossible in " << arg1
882 <<
"d or simply does not make any sense.");
894 <<
"You are trying to execute functionality that is "
895 <<
"impossible in dimensions <" << arg1 <<
',' << arg2
896 <<
"> or simply does not make any sense.");
903 "In a check in the code, deal.II encountered a zero in "
904 "a place where this does not make sense. See the condition "
905 "that was being checked and that is printed further up "
906 "in the error message to get more information on what "
907 "the erroneous zero corresponds to.");
914 "The object you are trying to access is empty but it makes "
915 "no sense to attempt the operation you are trying on an "
928 <<
"Dimension " << arg1 <<
" not equal to " << arg2 <<
'.');
938 <<
"Dimension " << arg1 <<
" neither equal to " << arg2
939 <<
" nor to " << arg3 <<
'.');
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 "
982 template <
typename T>
988 <<
"Index " << arg1 <<
" is not in the half-open range [" << arg2 <<
','
991 " In the current case, this half-open range is in fact empty, "
992 "suggesting that you are accessing an element of an empty "
993 "collection such as a vector that has not been set to the "
1003 <<
"Number " << arg1 <<
" must be larger than or equal "
1009 template <
typename T>
1013 <<
"Number " << arg1 <<
" must be larger than or equal "
1023 <<
"Division " << arg1 <<
" by " << arg2
1024 <<
" has remainder different from zero.");
1035 "You are trying to use an iterator, but the iterator is "
1036 "in an invalid state. This may indicate that the iterator "
1037 "object has not been initialized, or that it has been "
1038 "moved beyond the end of the range of valid elements.");
1045 "You are trying to use an iterator, but the iterator is "
1046 "pointing past the end of the range of valid elements. "
1047 "It is not valid to dereference the iterator in this "
1069 "You are trying an operation on a vector that is only "
1070 "allowed if the vector has no ghost elements, but the "
1071 "vector you are operating on does have ghost elements. "
1072 "Specifically, vectors with ghost elements are read-only "
1073 "and cannot appear in operations that write into these "
1076 "See the glossary entry on 'Ghosted vectors' for more "
1085 <<
"Something went wrong when making cell " << arg1
1086 <<
". Read the docs and the source code "
1087 <<
"for more information.");
1097 "You are trying an operation of the form 'vector = C', "
1098 "'matrix = C', or 'tensor = C' with a nonzero scalar value "
1099 "'C'. However, such assignments are only allowed if the "
1100 "C is zero, since the semantics for assigning any other "
1101 "value are not clear. For example: one could interpret "
1102 "assigning a matrix a value of 1 to mean the matrix has a "
1103 "norm of 1, the matrix is the identity matrix, or the "
1104 "matrix contains only 1s. Similar problems exist with "
1105 "vectors and tensors. Hence, to avoid this ambiguity, such "
1106 "assignments are not permitted.");
1113 "You are attempting to use functionality that is only available "
1114 "if deal.II was configured to use LAPACK, but cmake did not "
1115 "find a valid LAPACK library.");
1122 "You are attempting to use functionality that requires that deal.II is configured "
1123 "with HDF5 support. However, when you called 'cmake', HDF5 support "
1124 "was not detected.");
1131 "You are attempting to use functionality that is only available "
1132 "if deal.II was configured to use MPI.");
1139 "You are attempting to use functionality that is only available "
1140 "if deal.II was configured to use the function parser which "
1141 "relies on the muparser library, but cmake did not "
1142 "find a valid muparser library on your system and also did "
1143 "not choose the one that comes bundled with deal.II.");
1150 "You are attempting to use functionality that is only available "
1151 "if deal.II was configured to use Assimp, but cmake did not "
1152 "find a valid Assimp library.");
1154 #ifdef DEAL_II_WITH_CUDA
1167 <<
"There was an error in a cuSPARSE function: " << arg1);
1176 "You are attempting to use functionality that is only available if deal.II "
1177 "was configured to use Trilinos' SEACAS library (which provides ExodusII), "
1178 "but cmake did not find a valid SEACAS library.");
1185 "You are attempting to use functionality that is only available "
1186 "if deal.II was configured to use CGAL, but cmake did not "
1187 "find a valid CGAL library.");
1189 #ifdef DEAL_II_WITH_MPI
1217 print_info(std::ostream &out)
const override;
1225 #ifdef DEAL_II_TRILINOS_WITH_SEACAS
1242 ExcExodusII(
const int error_code);
1248 print_info(std::ostream &out)
const override;
1253 const int error_code;
1392 template <
class ExceptionType>
1397 const char *
function,
1399 const char * exc_name,
1403 e.set_fields(file, line,
function, cond, exc_name);
1442 template <
class ExceptionType>
1446 const char *
function,
1448 const char * exc_name,
1449 ExceptionType
e) noexcept
1451 static_assert(std::is_base_of<ExceptionBase, ExceptionType>::value,
1452 "The provided exception must inherit from ExceptionBase.");
1454 e.set_fields(file, line,
function, cond, exc_name);
1459 #ifdef DEAL_II_WITH_CUDA
1508 # ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1509 # define Assert(cond, exc) \
1511 if (__builtin_expect(!(cond), false)) \
1512 ::deal_II_exceptions::internals::issue_error_noreturn( \
1513 ::deal_II_exceptions::internals::ExceptionHandling:: \
1514 abort_or_throw_on_exception, \
1517 __PRETTY_FUNCTION__, \
1523 # define Assert(cond, exc) \
1526 ::deal_II_exceptions::internals::issue_error_noreturn( \
1527 ::deal_II_exceptions::internals::ExceptionHandling:: \
1528 abort_or_throw_on_exception, \
1531 __PRETTY_FUNCTION__, \
1538 # define Assert(cond, exc) \
1571 # ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1572 # define AssertNothrow(cond, exc) \
1574 if (__builtin_expect(!(cond), false)) \
1575 ::deal_II_exceptions::internals::issue_error_nothrow( \
1576 __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
1579 # define AssertNothrow(cond, exc) \
1582 ::deal_II_exceptions::internals::issue_error_nothrow( \
1583 __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
1587 # define AssertNothrow(cond, exc) \
1618 #ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1619 # define AssertThrow(cond, exc) \
1621 if (__builtin_expect(!(cond), false)) \
1622 ::deal_II_exceptions::internals::issue_error_noreturn( \
1623 ::deal_II_exceptions::internals::ExceptionHandling:: \
1624 throw_on_exception, \
1627 __PRETTY_FUNCTION__, \
1633 # define AssertThrow(cond, exc) \
1636 ::deal_II_exceptions::internals::issue_error_noreturn( \
1637 ::deal_II_exceptions::internals::ExceptionHandling:: \
1638 throw_on_exception, \
1641 __PRETTY_FUNCTION__, \
1658 template <
typename T,
typename U>
1659 inline constexpr
bool
1662 using common_type =
typename std::common_type<T, U>::type;
1663 return static_cast<common_type
>(t) ==
static_cast<common_type
>(u);
1672 template <
typename T,
typename U>
1673 inline constexpr
bool
1676 using common_type =
typename std::common_type<T, U>::type;
1677 return (
static_cast<common_type
>(t) <
static_cast<common_type
>(u));
1703 #define AssertDimension(dim1, dim2) \
1704 Assert(::deal_II_exceptions::internals::compare_for_equality(dim1, \
1706 ::ExcDimensionMismatch((dim1), (dim2)))
1726 #define AssertVectorVectorDimension(VEC, DIM1, DIM2) \
1727 AssertDimension(VEC.size(), DIM1); \
1728 for (const auto &subvector : VEC) \
1731 AssertDimension(subvector.size(), DIM2); \
1739 template <
typename T>
1742 template <
typename T,
typename U>
1768 #define AssertIndexRange(index, range) \
1770 ::deal_II_exceptions::internals::compare_less_than(index, range), \
1771 ExcIndexRangeType<typename ::internal::argument_type<void( \
1772 typename std::common_type<decltype(index), decltype(range)>::type)>:: \
1773 type>((index), 0, (range)))
1794 #define AssertIsFinite(number) \
1795 Assert(::numbers::is_finite(number), \
1796 ::ExcNumberNotFinite(std::complex<double>(number)))
1803 #define AssertIsNotUsed(obj) Assert((obj)->used() == false, ExcInternalError())
1805 #ifdef DEAL_II_WITH_MPI
1826 # define AssertThrowMPI(error_code) \
1827 AssertThrow(error_code == MPI_SUCCESS, ::ExcMPI(error_code))
1829 # define AssertThrowMPI(error_code) \
1833 #ifdef DEAL_II_WITH_CUDA
1852 # define AssertCuda(error_code) \
1853 Assert(error_code == cudaSuccess, \
1854 ::ExcCudaError(cudaGetErrorString(error_code)))
1856 # define AssertCuda(error_code) \
1858 (void)(error_code); \
1879 # define AssertNothrowCuda(error_code) \
1880 AssertNothrow(error_code == cudaSuccess, \
1881 ::ExcCudaError(cudaGetErrorString(error_code)))
1883 # define AssertNothrowCuda(error_code) \
1885 (void)(error_code); \
1907 # define AssertCudaKernel() \
1909 cudaError_t local_error_code = cudaPeekAtLastError(); \
1910 AssertCuda(local_error_code); \
1911 local_error_code = cudaDeviceSynchronize(); \
1912 AssertCuda(local_error_code) \
1915 # define AssertCudaKernel() \
1937 # define AssertCusparse(error_code) \
1939 error_code == CUSPARSE_STATUS_SUCCESS, \
1941 deal_II_exceptions::internals::get_cusparse_error_string( \
1944 # define AssertCusparse(error_code) \
1946 (void)(error_code); \
1967 # define AssertNothrowCusparse(error_code) \
1969 error_code == CUSPARSE_STATUS_SUCCESS, \
1971 deal_II_exceptions::internals::get_cusparse_error_string( \
1974 # define AssertNothrowCusparse(error_code) \
1976 (void)(error_code); \
1998 # define AssertCusolver(error_code) \
2000 error_code == CUSOLVER_STATUS_SUCCESS, \
2002 deal_II_exceptions::internals::get_cusolver_error_string( \
2005 # define AssertCusolver(error_code) \
2007 (void)(error_code); \
2013 #ifdef DEAL_II_TRILINOS_WITH_SEACAS
2031 # define AssertThrowExodusII(error_code) \
2032 AssertThrow(error_code == 0, ExcExodusII(error_code));
void generate_message() const
void print_stack_trace(std::ostream &out) const
const char * get_exc_name() const
virtual ~ExceptionBase() noexcept override=default
void set_fields(const char *file, const int line, const char *function, const char *cond, const char *exc_name)
virtual void print_info(std::ostream &out) const
virtual const char * what() const noexcept override
void print_exc_data(std::ostream &out) const
void * raw_stacktrace[25]
virtual void print_info(std::ostream &out) const override
ExcMPI(const int error_code)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcImpossibleInDimSpacedim(int arg1, int arg2)
#define DeclException0(Exception0)
static ::ExceptionBase & ExcZero()
static ::ExceptionBase & ExcGridHasInvalidCell(int arg1)
static ::ExceptionBase & ExcCudaError(const char *arg1)
static ::ExceptionBase & ExcLowerRange(int arg1, int arg2)
static ::ExceptionBase & ExcDivideByZero()
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcNeedsLAPACK()
static ::ExceptionBase & ExcNumberNotFinite(std::complex< double > arg1)
static ::ExceptionBase & ExcNeedsHDF5()
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcNeedsMPI()
static ::ExceptionBase & ExcFileNotOpen(std::string arg1)
static ::ExceptionBase & ExcOutOfMemory(std::size_t arg1)
static ::ExceptionBase & ExcNeedsAssimp()
static ::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
static ::ExceptionBase & ExcPureFunctionCalled()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcIteratorPastEnd()
static ::ExceptionBase & ExcInvalidIterator()
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcInvalidState()
static ::ExceptionBase & ExcImpossibleInDim(int arg1)
static ::ExceptionBase & ExcLowerRangeType(T arg1, T arg2)
#define DeclException2(Exception2, type1, type2, outsequence)
static ::ExceptionBase & ExcEmptyObject()
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcFunctionNotProvided(std::string arg1)
static ::ExceptionBase & ExcMemoryLeak(int arg1)
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcIO()
#define DeclException3(Exception3, type1, type2, type3, outsequence)
static ::ExceptionBase & ExcCusparseError(std::string arg1)
#define DeclException1(Exception1, type1, outsequence)
static ::ExceptionBase & ExcNeedsFunctionparser()
static ::ExceptionBase & ExcDimensionMismatch2(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcIndexRangeType(T arg1, T arg2, T arg3)
static ::ExceptionBase & ExcNeedsCGAL()
static ::ExceptionBase & ExcGhostsPresent()
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcNotMultiple(int arg1, int arg2)
static ::ExceptionBase & ExcNeedsExodusII()
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
std::string get_cusolver_error_string(const cusolverStatus_t error_code)
constexpr bool compare_less_than(const T &t, const U &u)
std::string get_cusparse_error_string(const cusparseStatus_t error_code)
constexpr bool compare_for_equality(const T &t, const U &u)
void issue_error_noreturn(ExceptionHandling handling, const char *file, int line, const char *function, const char *cond, const char *exc_name, ExceptionType e)
@ abort_or_throw_on_exception
void abort(const ExceptionBase &exc) noexcept
void do_issue_error_nothrow(const ExceptionBase &e) noexcept
bool allow_abort_on_exception
void issue_error_nothrow(const char *file, int line, const char *function, const char *cond, const char *exc_name, ExceptionType e) noexcept
void enable_abort_on_exception()
void disable_abort_on_exception()
void set_additional_assert_output(const char *const p)
void suppress_stacktrace_in_exceptions()