Reference documentation for deal.II version 9.6.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
exceptions.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 1998 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_exceptions_h
16#define dealii_exceptions_h
17
18#include <deal.II/base/config.h>
19
21#include <Kokkos_Macros.hpp>
22#if KOKKOS_VERSION >= 40200
23# include <Kokkos_Abort.hpp>
24#else
25# include <Kokkos_Core.hpp>
26#endif
28
29#include <exception>
30#include <ostream>
31#include <string>
32#include <type_traits>
33
34#ifdef DEAL_II_WITH_CUDA
35# include <cusolverSp.h>
36# include <cusparse.h>
37#endif
38
40
41
55class ExceptionBase : public std::exception
56{
57public:
62
67
71 virtual ~ExceptionBase() noexcept override = default;
72
78 operator=(const ExceptionBase &) = delete;
79
85 void
86 set_fields(const char *file,
87 const int line,
88 const char *function,
89 const char *cond,
90 const char *exc_name);
91
92
96 virtual const char *
97 what() const noexcept override;
98
102 const char *
103 get_exc_name() const;
104
108 void
109 print_exc_data(std::ostream &out) const;
110
115 virtual void
116 print_info(std::ostream &out) const;
117
122 void
123 print_stack_trace(std::ostream &out) const;
124
125protected:
129 const char *file;
130
134 unsigned int line;
135
139 const char *function;
140
144 const char *cond;
145
149 const char *exc;
150
156
157#ifdef DEAL_II_HAVE_GLIBC_STACKTRACE
161 void *raw_stacktrace[25];
162#endif
163
164private:
168 void
169 generate_message() const;
170
175 mutable std::string what_str;
176};
177
178#ifndef DOXYGEN
179
196# define DeclException0(Exception0) \
197 class Exception0 : public ::ExceptionBase \
198 {}
199
200
220# define DeclExceptionMsg(Exception, defaulttext) \
221 class Exception : public ::ExceptionBase \
222 { \
223 public: \
224 Exception(const std::string &msg = defaulttext) \
225 : arg(msg) \
226 {} \
227 virtual ~Exception() noexcept \
228 {} \
229 virtual void \
230 print_info(std::ostream &out) const override \
231 { \
232 out << " " << arg << std::endl; \
233 } \
234 \
235 private: \
236 const std::string arg; \
237 }
238
256# define DeclException1(Exception1, type1, outsequence) \
257 class Exception1 : public ::ExceptionBase \
258 { \
259 public: \
260 Exception1(type1 const &a1) \
261 : arg1(a1) \
262 {} \
263 virtual ~Exception1() noexcept \
264 {} \
265 virtual void \
266 print_info(std::ostream &out) const override \
267 { \
268 out << " " outsequence << std::endl; \
269 } \
270 \
271 private: \
272 type1 const arg1; \
273 }
274
275
293# define DeclException2(Exception2, type1, type2, outsequence) \
294 class Exception2 : public ::ExceptionBase \
295 { \
296 public: \
297 Exception2(type1 const &a1, type2 const &a2) \
298 : arg1(a1) \
299 , arg2(a2) \
300 {} \
301 virtual ~Exception2() noexcept \
302 {} \
303 virtual void \
304 print_info(std::ostream &out) const override \
305 { \
306 out << " " outsequence << std::endl; \
307 } \
308 \
309 private: \
310 type1 const arg1; \
311 type2 const arg2; \
312 }
313
314
332# define DeclException3(Exception3, type1, type2, type3, outsequence) \
333 class Exception3 : public ::ExceptionBase \
334 { \
335 public: \
336 Exception3(type1 const &a1, type2 const &a2, type3 const &a3) \
337 : arg1(a1) \
338 , arg2(a2) \
339 , arg3(a3) \
340 {} \
341 virtual ~Exception3() noexcept \
342 {} \
343 virtual void \
344 print_info(std::ostream &out) const override \
345 { \
346 out << " " outsequence << std::endl; \
347 } \
348 \
349 private: \
350 type1 const arg1; \
351 type2 const arg2; \
352 type3 const arg3; \
353 }
354
355
373# define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
374 class Exception4 : public ::ExceptionBase \
375 { \
376 public: \
377 Exception4(type1 const &a1, \
378 type2 const &a2, \
379 type3 const &a3, \
380 type4 const &a4) \
381 : arg1(a1) \
382 , arg2(a2) \
383 , arg3(a3) \
384 , arg4(a4) \
385 {} \
386 virtual ~Exception4() noexcept \
387 {} \
388 virtual void \
389 print_info(std::ostream &out) const override \
390 { \
391 out << " " outsequence << std::endl; \
392 } \
393 \
394 private: \
395 type1 const arg1; \
396 type2 const arg2; \
397 type3 const arg3; \
398 type4 const arg4; \
399 }
400
401
419# define DeclException5( \
420 Exception5, type1, type2, type3, type4, type5, outsequence) \
421 class Exception5 : public ::ExceptionBase \
422 { \
423 public: \
424 Exception5(type1 const &a1, \
425 type2 const &a2, \
426 type3 const &a3, \
427 type4 const &a4, \
428 type5 const &a5) \
429 : arg1(a1) \
430 , arg2(a2) \
431 , arg3(a3) \
432 , arg4(a4) \
433 , arg5(a5) \
434 {} \
435 virtual ~Exception5() noexcept \
436 {} \
437 virtual void \
438 print_info(std::ostream &out) const override \
439 { \
440 out << " " outsequence << std::endl; \
441 } \
442 \
443 private: \
444 type1 const arg1; \
445 type2 const arg2; \
446 type3 const arg3; \
447 type4 const arg4; \
448 type5 const arg5; \
449 }
450
451#else /*ifndef DOXYGEN*/
452
453// Dummy definitions for doxygen:
454
471# define DeclException0(Exception0) \
472 \
473 static ::ExceptionBase &Exception0()
474
494# define DeclExceptionMsg(Exception, defaulttext) \
495 \
496 \
497 static ::ExceptionBase &Exception()
498
516# define DeclException1(Exception1, type1, outsequence) \
517 \
518 \
519 static ::ExceptionBase &Exception1(type1 arg1)
520
521
539# define DeclException2(Exception2, type1, type2, outsequence) \
540 \
541 \
542 static ::ExceptionBase &Exception2(type1 arg1, type2 arg2)
543
544
562# define DeclException3(Exception3, type1, type2, type3, outsequence) \
563 \
564 \
565 static ::ExceptionBase &Exception3(type1 arg1, type2 arg2, type3 arg3)
566
567
585# define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
586 \
587 \
588 static ::ExceptionBase &Exception4(type1 arg1, \
589 type2 arg2, \
590 type3 arg3, \
591 type4 arg4)
592
593
611# define DeclException5( \
612 Exception5, type1, type2, type3, type4, type5, outsequence) \
613 \
614 \
615 static ::ExceptionBase &Exception5( \
616 type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)
617
618#endif /*ifndef DOXYGEN*/
619
620
633{
643 "A piece of code is attempting a division by zero. This is "
644 "likely going to lead to results that make no sense.");
645
658 std::complex<double>,
659 << "In a significant number of places, deal.II checks that some intermediate "
660 << "value is a finite number (as opposed to plus or minus infinity, or "
661 << "NaN/Not a Number). In the current function, we encountered a number "
662 << "that is not finite (its value is " << arg1 << " and therefore "
663 << "violates the current assertion).\n\n"
664 << "This may be due to the fact that some operation in this function "
665 << "created such a value, or because one of the arguments you passed "
666 << "to the function already had this value from some previous "
667 << "operation. In the latter case, this function only triggered the "
668 << "error but may not actually be responsible for the computation of "
669 << "the number that is not finite.\n\n"
670 << "There are two common cases where this situation happens. First, your "
671 << "code (or something in deal.II) divides by zero in a place where this "
672 << "should not happen. Or, you are trying to solve a linear system "
673 << "with an unsuitable solver (such as an indefinite or non-symmetric "
674 << "linear system using a Conjugate Gradient solver); such attempts "
675 << "oftentimes yield an operation somewhere that tries to divide "
676 << "by zero or take the square root of a negative value.\n\n"
677 << "In any case, when trying to find the source of the error, "
678 << "recall that the location where you are getting this error is "
679 << "simply the first place in the program where there is a check "
680 << "that a number (e.g., an element of a solution vector) is in fact "
681 << "finite, but that the actual error that computed the number "
682 << "may have happened far earlier. To find this location, you "
683 << "may want to add checks for finiteness in places of your "
684 << "program visited before the place where this error is produced. "
685 << "One way to check for finiteness is to use the 'AssertIsFinite' "
686 << "macro.");
687
692 std::size_t,
693 "Your program tried to allocate some memory but this "
694 "allocation failed. Typically, this either means that "
695 "you simply do not have enough memory in your system, "
696 "or that you are (erroneously) trying to allocate "
697 "a chunk of memory that is simply beyond all reasonable "
698 "size, for example because the size of the object has "
699 "been computed incorrectly."
700 "\n\n"
701 "In the current case, the request was for "
702 << arg1 << " bytes.");
703
709 int,
710 << "Destroying memory handler while " << arg1
711 << " objects are still allocated.");
712
717 "An input/output error has occurred. There are a number of "
718 "reasons why this may be happening, both for reading and "
719 "writing operations."
720 "\n\n"
721 "If this happens during an operation that tries to read "
722 "data: First, you may be "
723 "trying to read from a file that doesn't exist or that is "
724 "not readable given its file permissions. Second, deal.II "
725 "uses this error at times if it tries to "
726 "read information from a file but where the information "
727 "in the file does not correspond to the expected format. "
728 "An example would be a truncated file, or a mesh file "
729 "that contains not only sections that describe the "
730 "vertices and cells, but also sections for additional "
731 "data that deal.II does not understand."
732 "\n\n"
733 "If this happens during an operation that tries to write "
734 "data: you may be trying to write to a file to which file "
735 "or directory permissions do not allow you to write. A "
736 "typical example is where you specify an output file in "
737 "a directory that does not exist.");
738
746 std::string,
747 << "Could not open file " << arg1
748 << "."
749 "\n\n"
750 "If this happens during an operation that tries to read "
751 "data: you may be "
752 "trying to read from a file that doesn't exist or that is "
753 "not readable given its file permissions."
754 "\n\n"
755 "If this happens during an operation that tries to write "
756 "data: you may be trying to write to a file to which file "
757 "or directory permissions do not allow you to write. A "
758 "typical example is where you specify an output file in "
759 "a directory that does not exist.");
760
770 "You are trying to use functionality in deal.II that is "
771 "currently not implemented. In many cases, this indicates "
772 "that there simply didn't appear much of a need for it, or "
773 "that the author of the original code did not have the "
774 "time to implement a particular case. If you hit this "
775 "exception, it is therefore worth the time to look into "
776 "the code to find out whether you may be able to "
777 "implement the missing functionality. If you do, please "
778 "consider providing a patch to the deal.II development "
779 "sources (see the deal.II website on how to contribute).");
780
802 "This exception -- which is used in many places in the "
803 "library -- usually indicates that some condition which "
804 "the author of the code thought must be satisfied at a "
805 "certain point in an algorithm, is not fulfilled. An "
806 "example would be that the first part of an algorithm "
807 "sorts elements of an array in ascending order, and "
808 "a second part of the algorithm later encounters an "
809 "element that is not larger than the previous one."
810 "\n\n"
811 "There is usually not very much you can do if you "
812 "encounter such an exception since it indicates an error "
813 "in deal.II, not in your own program. Try to come up with "
814 "the smallest possible program that still demonstrates "
815 "the error and contact the deal.II mailing lists with it "
816 "to obtain help.");
817
826 "You (or a place in the library) are trying to call a "
827 "function that is declared as a virtual function in a "
828 "base class but that has not been overridden in your "
829 "derived class."
830 "\n\n"
831 "This exception happens in cases where the base class "
832 "cannot provide a useful default implementation for "
833 "the virtual function, but where we also do not want "
834 "to mark the function as abstract (i.e., with '=0' at the end) "
835 "because the function is not essential to the class in many "
836 "contexts. In cases like this, the base class provides "
837 "a dummy implementation that makes the compiler happy, but "
838 "that then throws the current exception."
839 "\n\n"
840 "A concrete example would be the 'Function' class. It declares "
841 "the existence of 'value()' and 'gradient()' member functions, "
842 "and both are marked as 'virtual'. Derived classes have to "
843 "override these functions for the values and gradients of a "
844 "particular function. On the other hand, not every function "
845 "has a gradient, and even for those that do, not every program "
846 "actually needs to evaluate it. Consequently, there is no "
847 "*requirement* that a derived class actually override the "
848 "'gradient()' function (as there would be had it been marked "
849 "as abstract). But, since the base class cannot know how to "
850 "compute the gradient, if a derived class does not override "
851 "the 'gradient()' function and it is called anyway, then the "
852 "default implementation in the base class will simply throw "
853 "an exception."
854 "\n\n"
855 "The exception you see is what happens in cases such as the "
856 "one just illustrated. To fix the problem, you need to "
857 "investigate whether the function being called should indeed have "
858 "been called; if the answer is 'yes', then you need to "
859 "implement the missing override in your class.");
860
865 std::string,
866 << "Please provide an implementation for the function \""
867 << arg1 << "\"");
868
874 std::string,
875 int,
876 << "The function \"" << arg1 << "\" returned the nonzero value " << arg2
877 << ", but the calling site expected the return value to be zero. "
878 "This error often happens when the function in question is a 'callback', "
879 "that is a user-provided function called from somewhere within deal.II "
880 "or within an external library such as PETSc, Trilinos, SUNDIALS, etc., "
881 "that expect these callbacks to indicate errors via nonzero return "
882 "codes.");
883
888
893
901 int,
902 << "You are trying to execute functionality that is "
903 << "impossible in " << arg1
904 << "d or simply does not make any sense.");
905
914 int,
915 int,
916 << "You are trying to execute functionality that is "
917 << "impossible in dimensions <" << arg1 << ',' << arg2
918 << "> or simply does not make any sense.");
919
920
925 "In a check in the code, deal.II encountered a zero in "
926 "a place where this does not make sense. See the condition "
927 "that was being checked and that is printed further up "
928 "in the error message to get more information on what "
929 "the erroneous zero corresponds to.");
930
936 "The object you are trying to access is empty but it makes "
937 "no sense to attempt the operation you are trying on an "
938 "empty object.");
939
948 std::size_t,
949 std::size_t,
950 << "Two sizes or dimensions were supposed to be equal, "
951 << "but aren't. They are " << arg1 << " and " << arg2 << '.');
952
958 std::size_t,
959 std::size_t,
960 std::size_t,
961 << "The size or dimension of one object, " << arg1
962 << " was supposed to be "
963 << "equal to one of two values, but isn't. The two possible "
964 << "values are " << arg2 << " and " << arg3 << '.');
965
980 std::size_t,
981 std::size_t,
982 std::size_t,
983 << "Index " << arg1 << " is not in the half-open range [" << arg2 << ','
984 << arg3 << ")."
985 << (arg2 == arg3 ?
986 " In the current case, this half-open range is in fact empty, "
987 "suggesting that you are accessing an element of an empty "
988 "collection such as a vector that has not been set to the "
989 "correct size." :
990 ""));
991
1007 template <typename T>
1010 T,
1011 T,
1012 T,
1013 << "Index " << arg1 << " is not in the half-open range [" << arg2 << ','
1014 << arg3 << ")."
1015 << (arg2 == arg3 ?
1016 " In the current case, this half-open range is in fact empty, "
1017 "suggesting that you are accessing an element of an empty "
1018 "collection such as a vector that has not been set to the "
1019 "correct size." :
1020 ""));
1021
1026 int,
1027 int,
1028 << "Number " << arg1 << " must be larger than or equal "
1029 << arg2 << '.');
1030
1034 template <typename T>
1036 T,
1037 T,
1038 << "Number " << arg1 << " must be larger than or equal "
1039 << arg2 << '.');
1040
1046 int,
1047 int,
1048 << "Division " << arg1 << " by " << arg2
1049 << " has remainder different from zero.");
1050
1060 "You are trying to use an iterator, but the iterator is "
1061 "in an invalid state. This may indicate that the iterator "
1062 "object has not been initialized, or that it has been "
1063 "moved beyond the end of the range of valid elements.");
1064
1070 "You are trying to use an iterator, but the iterator is "
1071 "pointing past the end of the range of valid elements. "
1072 "It is not valid to dereference the iterator in this "
1073 "case.");
1074
1088 DeclException1(ExcMessage, std::string, << arg1);
1089
1094 "You are trying an operation on a vector that is only "
1095 "allowed if the vector has no ghost elements, but the "
1096 "vector you are operating on does have ghost elements."
1097 "\n\n"
1098 "Specifically, there are two kinds of operations that "
1099 "are typically not allowed on vectors with ghost elements. "
1100 "First, vectors with ghost elements are read-only "
1101 "and cannot appear in operations that write into these "
1102 "vectors. Second, reduction operations (such as computing "
1103 "the norm of a vector, or taking dot products between "
1104 "vectors) are not allowed to ensure that each vector "
1105 "element is counted only once (as opposed to once for "
1106 "the owner of the element plus once for each process "
1107 "on which the element is stored as a ghost copy)."
1108 "\n\n"
1109 "See the glossary entry on 'Ghosted vectors' for more "
1110 "information.");
1111
1117 int,
1118 << "Something went wrong when making cell " << arg1
1119 << ". Read the docs and the source code "
1120 << "for more information.");
1121
1130 "You are trying an operation of the form 'vector = C', "
1131 "'matrix = C', or 'tensor = C' with a nonzero scalar value "
1132 "'C'. However, such assignments are only allowed if the "
1133 "C is zero, since the semantics for assigning any other "
1134 "value are not clear. For example: one could interpret "
1135 "assigning a matrix a value of 1 to mean the matrix has a "
1136 "norm of 1, the matrix is the identity matrix, or the "
1137 "matrix contains only 1s. Similar problems exist with "
1138 "vectors and tensors. Hence, to avoid this ambiguity, such "
1139 "assignments are not permitted.");
1140
1146 "You are attempting to use functionality that is only available "
1147 "if deal.II was configured to use LAPACK, but cmake did not "
1148 "find a valid LAPACK library.");
1149
1155 "You are attempting to use functionality that requires that deal.II is configured "
1156 "with HDF5 support. However, when you called 'cmake', HDF5 support "
1157 "was not detected.");
1158
1164 "You are attempting to use functionality that is only available "
1165 "if deal.II was configured to use MPI.");
1166
1172 "You are attempting to use functionality that is only available "
1173 "if deal.II was configured to use the function parser which "
1174 "relies on the muparser library, but cmake did not "
1175 "find a valid muparser library on your system and also did "
1176 "not choose the one that comes bundled with deal.II.");
1177
1183 "You are attempting to use functionality that is only available "
1184 "if deal.II was configured to use Assimp, but cmake did not "
1185 "find a valid Assimp library.");
1186
1187#ifdef DEAL_II_WITH_CUDA
1194 DeclException1(ExcCudaError, const char *, << arg1);
1199 std::string,
1200 << "There was an error in a cuSPARSE function: " << arg1);
1201#endif
1209 "You are attempting to use functionality that is only available if deal.II "
1210 "was configured to use Trilinos' SEACAS library (which provides ExodusII), "
1211 "but cmake did not find a valid SEACAS library.");
1212
1218 "You are attempting to use functionality that is only available "
1219 "if deal.II was configured to use CGAL, but cmake did not "
1220 "find a valid CGAL library.");
1221
1222#ifdef DEAL_II_WITH_MPI
1245 {
1246 public:
1247 ExcMPI(const int error_code);
1248
1249 virtual void
1250 print_info(std::ostream &out) const override;
1251
1252 const int error_code;
1253 };
1254#endif // DEAL_II_WITH_MPI
1255
1256
1257
1258#ifdef DEAL_II_TRILINOS_WITH_SEACAS
1267 class ExcExodusII : public ExceptionBase
1268 {
1269 public:
1275 ExcExodusII(const int error_code);
1276
1280 virtual void
1281 print_info(std::ostream &out) const override;
1282
1286 const int error_code;
1287 };
1288#endif // DEAL_II_TRILINOS_WITH_SEACAS
1289
1296 "A user call-back function encountered a recoverable error, "
1297 "but the underlying library that called the call-back did not "
1298 "manage to recover from the error and aborted its operation."
1299 "\n\n"
1300 "See the glossary entry on user call-back functions for more "
1301 "information.");
1302} /*namespace StandardExceptions*/
1303
1304
1305
1313{
1314 namespace internals
1315 {
1323 extern bool allow_abort_on_exception;
1324 } // namespace internals
1325
1344 void
1345 set_additional_assert_output(const char *const p);
1346
1357 void
1359
1374 void
1376
1385 void
1387
1395 namespace internals
1396 {
1401 [[noreturn]] void
1402 abort(const ExceptionBase &exc) noexcept;
1403
1420
1438 template <typename ExceptionType>
1439 [[noreturn]] void
1441 const char *file,
1442 int line,
1443 const char *function,
1444 const char *cond,
1445 const char *exc_name,
1446 ExceptionType e)
1447 {
1448 static_assert(std::is_base_of_v<ExceptionBase, ExceptionType>,
1449 "The provided exception must inherit from ExceptionBase.");
1450 // Fill the fields of the exception object
1451 e.set_fields(file, line, function, cond, exc_name);
1452
1453 switch (handling)
1454 {
1456 {
1457 if (::deal_II_exceptions::internals::
1460 else
1461 {
1462 // We are not allowed to abort, so just throw the error:
1463 throw e;
1464 }
1465 }
1467 throw e;
1468 // this function should never return (and AssertNothrow can);
1469 // something must have gone wrong in the error handling code for us
1470 // to get this far, so throw an exception.
1471 default:
1472 throw ::StandardExceptions::ExcInternalError();
1473 }
1474 }
1475
1479 void
1480 do_issue_error_nothrow(const ExceptionBase &e) noexcept;
1481
1490 template <typename ExceptionType>
1491 void
1492 issue_error_nothrow(const char *file,
1493 int line,
1494 const char *function,
1495 const char *cond,
1496 const char *exc_name,
1497 ExceptionType e) noexcept
1498 {
1499 static_assert(std::is_base_of_v<ExceptionBase, ExceptionType>,
1500 "The provided exception must inherit from ExceptionBase.");
1501 // Fill the fields of the exception object
1502 e.set_fields(file, line, function, cond, exc_name);
1503 // avoid moving a bunch of code into the header by dispatching to
1504 // another function:
1506 }
1507#ifdef DEAL_II_WITH_CUDA
1513 std::string
1514 get_cusparse_error_string(const cusparseStatus_t error_code);
1515
1521 std::string
1522 get_cusolver_error_string(const cusolverStatus_t error_code);
1523#endif
1524 } /*namespace internals*/
1525
1526} /*namespace deal_II_exceptions*/
1527
1528
1529
1555#ifdef DEBUG
1556# if KOKKOS_VERSION >= 30600
1557# ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1558# define Assert(cond, exc) \
1559 do \
1560 { \
1561 KOKKOS_IF_ON_HOST(({ \
1562 if (__builtin_expect(!(cond), false)) \
1563 ::deal_II_exceptions::internals::issue_error_noreturn( \
1564 ::deal_II_exceptions::internals::ExceptionHandling:: \
1565 abort_or_throw_on_exception, \
1566 __FILE__, \
1567 __LINE__, \
1568 __PRETTY_FUNCTION__, \
1569 #cond, \
1570 #exc, \
1571 exc); \
1572 })) \
1573 KOKKOS_IF_ON_DEVICE(({ \
1574 if (!(cond)) \
1575 Kokkos::abort(#cond); \
1576 })) \
1577 } \
1578 while (false)
1579# else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1580# define Assert(cond, exc) \
1581 do \
1582 { \
1583 KOKKOS_IF_ON_HOST(({ \
1584 if (!(cond)) \
1585 ::deal_II_exceptions::internals::issue_error_noreturn( \
1586 ::deal_II_exceptions::internals::ExceptionHandling:: \
1587 abort_or_throw_on_exception, \
1588 __FILE__, \
1589 __LINE__, \
1590 __PRETTY_FUNCTION__, \
1591 #cond, \
1592 #exc, \
1593 exc); \
1594 })) \
1595 KOKKOS_IF_ON_DEVICE(({ \
1596 if (!(cond)) \
1597 Kokkos::abort(#cond); \
1598 })) \
1599 } \
1600 while (false)
1601# endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1602# else /*if KOKKOS_VERSION >= 30600*/
1603# ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
1604# ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1605# define Assert(cond, exc) \
1606 do \
1607 { \
1608 if (__builtin_expect(!(cond), false)) \
1609 ::deal_II_exceptions::internals::issue_error_noreturn( \
1610 ::deal_II_exceptions::internals::ExceptionHandling:: \
1611 abort_or_throw_on_exception, \
1612 __FILE__, \
1613 __LINE__, \
1614 __PRETTY_FUNCTION__, \
1615 #cond, \
1616 #exc, \
1617 exc); \
1618 } \
1619 while (false)
1620# else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1621# define Assert(cond, exc) \
1622 do \
1623 { \
1624 if (!(cond)) \
1625 ::deal_II_exceptions::internals::issue_error_noreturn( \
1626 ::deal_II_exceptions::internals::ExceptionHandling:: \
1627 abort_or_throw_on_exception, \
1628 __FILE__, \
1629 __LINE__, \
1630 __PRETTY_FUNCTION__, \
1631 #cond, \
1632 #exc, \
1633 exc); \
1634 } \
1635 while (false)
1636# endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1637# else /*#ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
1638# define Assert(cond, exc) \
1639 do \
1640 { \
1641 if (!(cond)) \
1642 Kokkos::abort(#cond); \
1643 } \
1644 while (false)
1645# endif /*ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
1646# endif /*KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
1647#else /*ifdef DEBUG*/
1648# define Assert(cond, exc) \
1649 do \
1650 { \
1651 } \
1652 while (false)
1653#endif /*ifdef DEBUG*/
1654
1655
1656
1683#ifdef DEBUG
1684# ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1685# define AssertNothrow(cond, exc) \
1686 do \
1687 { \
1688 if (__builtin_expect(!(cond), false)) \
1689 ::deal_II_exceptions::internals::issue_error_nothrow( \
1690 __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
1691 } \
1692 while (false)
1693# else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1694# define AssertNothrow(cond, exc) \
1695 do \
1696 { \
1697 if (!(cond)) \
1698 ::deal_II_exceptions::internals::issue_error_nothrow( \
1699 __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
1700 } \
1701 while (false)
1702# endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1703#else
1704# define AssertNothrow(cond, exc) \
1705 do \
1706 { \
1707 } \
1708 while (false)
1709#endif
1710
1738#ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1739# define AssertThrow(cond, exc) \
1740 do \
1741 { \
1742 if (__builtin_expect(!(cond), false)) \
1743 ::deal_II_exceptions::internals::issue_error_noreturn( \
1744 ::deal_II_exceptions::internals::ExceptionHandling:: \
1745 throw_on_exception, \
1746 __FILE__, \
1747 __LINE__, \
1748 __PRETTY_FUNCTION__, \
1749 #cond, \
1750 #exc, \
1751 exc); \
1752 } \
1753 while (false)
1754#else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1755# define AssertThrow(cond, exc) \
1756 do \
1757 { \
1758 if (!(cond)) \
1759 ::deal_II_exceptions::internals::issue_error_noreturn( \
1760 ::deal_II_exceptions::internals::ExceptionHandling:: \
1761 throw_on_exception, \
1762 __FILE__, \
1763 __LINE__, \
1764 __PRETTY_FUNCTION__, \
1765 #cond, \
1766 #exc, \
1767 exc); \
1768 } \
1769 while (false)
1770#endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1771
1772
1814#define DEAL_II_NOT_IMPLEMENTED() \
1815 ::deal_II_exceptions::internals::issue_error_noreturn( \
1816 ::deal_II_exceptions::internals::ExceptionHandling:: \
1817 abort_or_throw_on_exception, \
1818 __FILE__, \
1819 __LINE__, \
1820 __PRETTY_FUNCTION__, \
1821 nullptr, \
1822 nullptr, \
1823 ::StandardExceptions::ExcNotImplemented())
1824
1825
1897#define DEAL_II_ASSERT_UNREACHABLE() \
1898 ::deal_II_exceptions::internals::issue_error_noreturn( \
1899 ::deal_II_exceptions::internals::ExceptionHandling:: \
1900 abort_or_throw_on_exception, \
1901 __FILE__, \
1902 __LINE__, \
1903 __PRETTY_FUNCTION__, \
1904 nullptr, \
1905 nullptr, \
1906 ::StandardExceptions::ExcMessage( \
1907 "The program has hit a line of code that the programmer " \
1908 "marked with the macro DEAL_II_ASSERT_UNREACHABLE() to " \
1909 "indicate that the program should never reach this " \
1910 "location. You will have to find out (best done in a " \
1911 "debugger) why that happened. Typical reasons include " \
1912 "passing invalid arguments to functions (for example, if " \
1913 "a function takes an 'enum' with two possible values " \
1914 "as argument, but you call the function with a third " \
1915 "value), or if the programmer of the code that triggered " \
1916 "the error believed that a variable can only have " \
1917 "specific values, but either that assumption is wrong " \
1918 "or the computation of that value is buggy." \
1919 "\n\n" \
1920 "In those latter conditions, where some internal " \
1921 "assumption is not satisfied, there may not be very " \
1922 "much you can do if you encounter such an exception, " \
1923 "since it indicates an error in deal.II, not in your " \
1924 "own program. If that is the situation you encounter, " \
1925 "try to come up with " \
1926 "the smallest possible program that still demonstrates " \
1927 "the error and contact the deal.II mailing lists with it " \
1928 "to obtain help."))
1929
1930
1931namespace deal_II_exceptions
1932{
1933 namespace internals
1934 {
1940 template <typename T, typename U>
1941 inline DEAL_II_HOST_DEVICE constexpr bool
1942 compare_for_equality(const T &t, const U &u)
1943 {
1944 using common_type = std::common_type_t<T, U>;
1945 return static_cast<common_type>(t) == static_cast<common_type>(u);
1946 }
1947
1948
1954 template <typename T, typename U>
1955 inline DEAL_II_HOST_DEVICE constexpr bool
1956 compare_less_than(const T &t, const U &u)
1957 {
1958 using common_type = std::common_type_t<T, U>;
1959 return (static_cast<common_type>(t) < static_cast<common_type>(u));
1960 }
1961 } // namespace internals
1962} // namespace deal_II_exceptions
1963
1964
1985#define AssertDimension(dim1, dim2) \
1986 Assert(::deal_II_exceptions::internals::compare_for_equality(dim1, \
1987 dim2), \
1988 ::ExcDimensionMismatch((dim1), (dim2)))
1989
1990
2008#define AssertVectorVectorDimension(VEC, DIM1, DIM2) \
2009 AssertDimension(VEC.size(), DIM1); \
2010 for (const auto &subvector : VEC) \
2011 { \
2012 (void)subvector; \
2013 AssertDimension(subvector.size(), DIM2); \
2014 }
2015
2016namespace internal
2017{
2018 // Workaround to allow for commas in template parameter lists
2019 // in preprocessor macros as found in
2020 // https://stackoverflow.com/questions/13842468/comma-in-c-c-macro
2021 template <typename F>
2023
2024 template <typename T, typename U>
2025 struct argument_type<T(U)>
2026 {
2027 using type = U;
2028 };
2029
2030 template <typename F>
2032} // namespace internal
2033
2053#define AssertIndexRange(index, range) \
2054 Assert(::deal_II_exceptions::internals::compare_less_than(index, \
2055 range), \
2056 ExcIndexRangeType<::internal::argument_type_t<void( \
2057 std::common_type_t<decltype(index), decltype(range)>)>>((index), \
2058 0, \
2059 (range)))
2060
2080#define AssertIsFinite(number) \
2081 Assert(::numbers::is_finite(number), \
2082 ::ExcNumberNotFinite(std::complex<double>(number)))
2083
2089#define AssertIsNotUsed(obj) Assert((obj)->used() == false, ExcInternalError())
2090
2091#ifdef DEAL_II_WITH_MPI
2112# define AssertThrowMPI(error_code) \
2113 AssertThrow(error_code == MPI_SUCCESS, ::ExcMPI(error_code))
2114#else
2115# define AssertThrowMPI(error_code) \
2116 do \
2117 { \
2118 } \
2119 while (false)
2120#endif // DEAL_II_WITH_MPI
2121
2122#ifdef DEAL_II_WITH_CUDA
2140# ifdef DEBUG
2141# define AssertCuda(error_code) \
2142 Assert(error_code == cudaSuccess, \
2143 ::ExcCudaError(cudaGetErrorString(error_code)))
2144# else
2145# define AssertCuda(error_code) \
2146 do \
2147 { \
2148 (void)(error_code); \
2149 } \
2150 while (false)
2151# endif
2152
2169# ifdef DEBUG
2170# define AssertNothrowCuda(error_code) \
2171 AssertNothrow(error_code == cudaSuccess, \
2172 ::ExcCudaError(cudaGetErrorString(error_code)))
2173# else
2174# define AssertNothrowCuda(error_code) \
2175 do \
2176 { \
2177 (void)(error_code); \
2178 } \
2179 while (false)
2180# endif
2181
2199# ifdef DEBUG
2200# define AssertCudaKernel() \
2201 do \
2202 { \
2203 cudaError_t local_error_code = cudaPeekAtLastError(); \
2204 AssertCuda(local_error_code); \
2205 local_error_code = cudaDeviceSynchronize(); \
2206 AssertCuda(local_error_code); \
2207 } \
2208 while (false)
2209# else
2210# define AssertCudaKernel() \
2211 do \
2212 { \
2213 } \
2214 while (false)
2215# endif
2216
2234# ifdef DEBUG
2235# define AssertCusparse(error_code) \
2236 Assert( \
2237 error_code == CUSPARSE_STATUS_SUCCESS, \
2238 ExcCusparseError( \
2239 deal_II_exceptions::internals::get_cusparse_error_string( \
2240 error_code)))
2241# else
2242# define AssertCusparse(error_code) \
2243 do \
2244 { \
2245 (void)(error_code); \
2246 } \
2247 while (false)
2248# endif
2249
2266# ifdef DEBUG
2267# define AssertNothrowCusparse(error_code) \
2268 AssertNothrow( \
2269 error_code == CUSPARSE_STATUS_SUCCESS, \
2270 ExcCusparseError( \
2271 deal_II_exceptions::internals::get_cusparse_error_string( \
2272 error_code)))
2273# else
2274# define AssertNothrowCusparse(error_code) \
2275 do \
2276 { \
2277 (void)(error_code); \
2278 } \
2279 while (false)
2280# endif
2281
2299# ifdef DEBUG
2300# define AssertCusolver(error_code) \
2301 Assert( \
2302 error_code == CUSOLVER_STATUS_SUCCESS, \
2303 ExcCusparseError( \
2304 deal_II_exceptions::internals::get_cusolver_error_string( \
2305 error_code)))
2306# else
2307# define AssertCusolver(error_code) \
2308 do \
2309 { \
2310 (void)(error_code); \
2311 } \
2312 while (false)
2313# endif
2314
2315#endif
2316
2317#ifdef DEAL_II_TRILINOS_WITH_SEACAS
2335# define AssertThrowExodusII(error_code) \
2336 AssertThrow(error_code == 0, \
2337 ::StandardExceptions::ExcExodusII(error_code));
2338#endif // DEAL_II_TRILINOS_WITH_SEACAS
2339
2340using namespace StandardExceptions;
2341
2343
2344#endif
void generate_message() const
void print_stack_trace(std::ostream &out) const
const char * file
Definition exceptions.h:129
std::string what_str
Definition exceptions.h:175
const char * get_exc_name() const
virtual ~ExceptionBase() noexcept override=default
int n_stacktrace_frames
Definition exceptions.h:155
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
const char * exc
Definition exceptions.h:149
const char * function
Definition exceptions.h:139
void print_exc_data(std::ostream &out) const
unsigned int line
Definition exceptions.h:134
void * raw_stacktrace[25]
Definition exceptions.h:161
const char * cond
Definition exceptions.h:144
virtual void print_info(std::ostream &out) const override
ExcMPI(const int error_code)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:503
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
Definition config.h:517
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:504
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
Definition config.h:560
static ::ExceptionBase & ExcIndexRangeType(T arg1, T arg2, T arg3)
static ::ExceptionBase & ExcImpossibleInDimSpacedim(int arg1, int arg2)
#define DeclException0(Exception0)
Definition exceptions.h:471
static ::ExceptionBase & ExcInvalidIterator()
static ::ExceptionBase & ExcOutOfMemory(std::size_t arg1)
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcGhostsPresent()
static ::ExceptionBase & ExcFileNotOpen(std::string arg1)
static ::ExceptionBase & ExcZero()
static ::ExceptionBase & ExcNeedsAssimp()
static ::ExceptionBase & ExcNeedsFunctionparser()
static ::ExceptionBase & ExcNotMultiple(int arg1, int arg2)
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcNeedsMPI()
static ::ExceptionBase & ExcFunctionNotProvided(std::string arg1)
static ::ExceptionBase & ExcEmptyObject()
static ::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
static ::ExceptionBase & ExcMemoryLeak(int arg1)
static ::ExceptionBase & ExcLowerRangeType(T arg1, T arg2)
static ::ExceptionBase & ExcImpossibleInDim(int arg1)
static ::ExceptionBase & ExcCusparseError(std::string arg1)
static ::ExceptionBase & ExcIteratorPastEnd()
static ::ExceptionBase & ExcNeedsExodusII()
static ::ExceptionBase & ExcDimensionMismatch2(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcCudaError(const char *arg1)
#define DeclException2(Exception2, type1, type2, outsequence)
Definition exceptions.h:539
static ::ExceptionBase & ExcLowerRange(int arg1, int arg2)
static ::ExceptionBase & ExcGridHasInvalidCell(int arg1)
static ::ExceptionBase & ExcNumberNotFinite(std::complex< double > arg1)
static ::ExceptionBase & ExcPureFunctionCalled()
#define DeclExceptionMsg(Exception, defaulttext)
Definition exceptions.h:494
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDivideByZero()
static ::ExceptionBase & ExcNeedsHDF5()
static ::ExceptionBase & ExcNeedsLAPACK()
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition exceptions.h:562
static ::ExceptionBase & ExcNeedsCGAL()
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcInvalidState()
#define DeclException1(Exception1, type1, outsequence)
Definition exceptions.h:516
static ::ExceptionBase & RecoverableUserCallbackError()
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcFunctionNonzeroReturn(std::string arg1, int arg2)
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)
void abort(const ExceptionBase &exc) noexcept
void do_issue_error_nothrow(const ExceptionBase &e) noexcept
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()
Definition exceptions.cc:88
void disable_abort_on_exception()
Definition exceptions.cc:80
void set_additional_assert_output(const char *const p)
void suppress_stacktrace_in_exceptions()
Definition exceptions.cc:72
typename argument_type< F >::type argument_type_t
STL namespace.
#define DEAL_II_HOST_DEVICE
Definition numbers.h:34