deal.II version GIT relicensing-6809-ge913b9bb34 2026-09-25 17:20:01+00:00
\(\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
exception_macros.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2012 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_exception_macros_h
14#define dealii_exception_macros_h
15
16#include <deal.II/base/config.h>
17
18
19/**********************************************************************
20 * Preprocessor definitions in support of declaring exception classes.
21 *
22 * These make reference to classes and functions that are declared in
23 * exceptions.h. This is ok as far as concerning preprocessor defines
24 * is concerned, but in order to use the material below, you also need
25 * to have `#include <deal.II/base/exceptions.h>`.
26 */
27#ifndef DOXYGEN
28
45# define DeclException0(Exception0) \
46 class Exception0 : public ::ExceptionBase \
47 {}
48
49
69// We explicitly assign msg to arg (instead of using the member initializer list
70// syntax) to work around a bug in nvcc 13.1 and earlier when using C++20.
71# define DeclExceptionMsg(Exception, defaulttext) \
72 class Exception : public ::ExceptionBase \
73 { \
74 public: \
75 Exception(const std::string &msg = defaulttext) \
76 { \
77 arg = msg; \
78 } \
79 virtual ~Exception() noexcept \
80 {} \
81 virtual void \
82 print_info(std::ostream &out) const override \
83 { \
84 out << " " << arg << std::endl; \
85 } \
86 \
87 private: \
88 std::string arg; \
89 }
90
108# define DeclException1(Exception1, type1, outsequence) \
109 class Exception1 : public ::ExceptionBase \
110 { \
111 public: \
112 Exception1(type1 const &a1) \
113 : arg1(a1) \
114 {} \
115 virtual ~Exception1() noexcept \
116 {} \
117 virtual void \
118 print_info(std::ostream &out) const override \
119 { \
120 out << " " outsequence << std::endl; \
121 } \
122 \
123 private: \
124 type1 const arg1; \
125 }
126
127
145# define DeclException2(Exception2, type1, type2, outsequence) \
146 class Exception2 : public ::ExceptionBase \
147 { \
148 public: \
149 Exception2(type1 const &a1, type2 const &a2) \
150 : arg1(a1) \
151 , arg2(a2) \
152 {} \
153 virtual ~Exception2() noexcept \
154 {} \
155 virtual void \
156 print_info(std::ostream &out) const override \
157 { \
158 out << " " outsequence << std::endl; \
159 } \
160 \
161 private: \
162 type1 const arg1; \
163 type2 const arg2; \
164 }
165
166
184# define DeclException3(Exception3, type1, type2, type3, outsequence) \
185 class Exception3 : public ::ExceptionBase \
186 { \
187 public: \
188 Exception3(type1 const &a1, type2 const &a2, type3 const &a3) \
189 : arg1(a1) \
190 , arg2(a2) \
191 , arg3(a3) \
192 {} \
193 virtual ~Exception3() noexcept \
194 {} \
195 virtual void \
196 print_info(std::ostream &out) const override \
197 { \
198 out << " " outsequence << std::endl; \
199 } \
200 \
201 private: \
202 type1 const arg1; \
203 type2 const arg2; \
204 type3 const arg3; \
205 }
206
207
225# define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
226 class Exception4 : public ::ExceptionBase \
227 { \
228 public: \
229 Exception4(type1 const &a1, \
230 type2 const &a2, \
231 type3 const &a3, \
232 type4 const &a4) \
233 : arg1(a1) \
234 , arg2(a2) \
235 , arg3(a3) \
236 , arg4(a4) \
237 {} \
238 virtual ~Exception4() noexcept \
239 {} \
240 virtual void \
241 print_info(std::ostream &out) const override \
242 { \
243 out << " " outsequence << std::endl; \
244 } \
245 \
246 private: \
247 type1 const arg1; \
248 type2 const arg2; \
249 type3 const arg3; \
250 type4 const arg4; \
251 }
252
253
271# define DeclException5( \
272 Exception5, type1, type2, type3, type4, type5, outsequence) \
273 class Exception5 : public ::ExceptionBase \
274 { \
275 public: \
276 Exception5(type1 const &a1, \
277 type2 const &a2, \
278 type3 const &a3, \
279 type4 const &a4, \
280 type5 const &a5) \
281 : arg1(a1) \
282 , arg2(a2) \
283 , arg3(a3) \
284 , arg4(a4) \
285 , arg5(a5) \
286 {} \
287 virtual ~Exception5() noexcept \
288 {} \
289 virtual void \
290 print_info(std::ostream &out) const override \
291 { \
292 out << " " outsequence << std::endl; \
293 } \
294 \
295 private: \
296 type1 const arg1; \
297 type2 const arg2; \
298 type3 const arg3; \
299 type4 const arg4; \
300 type5 const arg5; \
301 }
302
303#else /*ifndef DOXYGEN*/
304
305// Dummy definitions for doxygen:
306
323# define DeclException0(Exception0) \
324 \
325 static ::ExceptionBase &Exception0()
326
346# define DeclExceptionMsg(Exception, defaulttext) \
347 \
348 \
349 static ::ExceptionBase &Exception()
350
368# define DeclException1(Exception1, type1, outsequence) \
369 \
370 \
371 static ::ExceptionBase &Exception1(type1 arg1)
372
373
391# define DeclException2(Exception2, type1, type2, outsequence) \
392 \
393 \
394 static ::ExceptionBase &Exception2(type1 arg1, type2 arg2)
395
396
414# define DeclException3(Exception3, type1, type2, type3, outsequence) \
415 \
416 \
417 static ::ExceptionBase &Exception3(type1 arg1, type2 arg2, type3 arg3)
418
419
437# define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
438 \
439 \
440 static ::ExceptionBase &Exception4(type1 arg1, \
441 type2 arg2, \
442 type3 arg3, \
443 type4 arg4)
444
445
463# define DeclException5( \
464 Exception5, type1, type2, type3, type4, type5, outsequence) \
465 \
466 \
467 static ::ExceptionBase &Exception5( \
468 type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)
469
470#endif /*ifndef DOXYGEN*/
471
472
473
474/**********************************************************************
475 * Preprocessor definitions in support of assertions.
476 *
477 * These make reference to classes and functions that are declared in
478 * exceptions.h. This is ok as far as concerning preprocessor defines
479 * is concerned, but in order to use the material below, you also need
480 * to have `#include <deal.II/base/exceptions.h>`.
481 */
482
487#ifdef DEAL_II_HAVE_BUILTIN_EXPECT
488# define DEAL_II_BUILTIN_EXPECT(a, b) __builtin_expect((a), (b))
489#else
490# define DEAL_II_BUILTIN_EXPECT(a, b) (a)
491#endif
492
493
519#ifdef DEBUG
520# if DEAL_II_KOKKOS_VERSION_GTE(3, 6, 0)
521# define Assert(cond, exc) \
522 do \
523 { \
524 KOKKOS_IF_ON_HOST(({ \
525 if (DEAL_II_BUILTIN_EXPECT(!(cond), false)) \
526 ::deal_II_exceptions::internals::issue_error_noreturn( \
527 ::deal_II_exceptions::internals::ExceptionHandling:: \
528 abort_or_throw_on_exception, \
529 __FILE__, \
530 __LINE__, \
531 __PRETTY_FUNCTION__, \
532 #cond, \
533 #exc, \
534 exc); \
535 })) \
536 KOKKOS_IF_ON_DEVICE(({ \
537 if (!(cond)) \
538 Kokkos::abort(#cond); \
539 })) \
540 } \
541 while (false)
542# else /*if DEAL_II_KOKKOS_VERSION_GTE(3,6,0), no device support: */
543# define Assert(cond, exc) \
544 do \
545 { \
546 if (DEAL_II_BUILTIN_EXPECT(!(cond), false)) \
547 ::deal_II_exceptions::internals::issue_error_noreturn( \
548 ::deal_II_exceptions::internals::ExceptionHandling:: \
549 abort_or_throw_on_exception, \
550 __FILE__, \
551 __LINE__, \
552 __PRETTY_FUNCTION__, \
553 #cond, \
554 #exc, \
555 exc); \
556 } \
557 while (false)
558# endif /*DEAL_II_KOKKOS_VERSION_GTE(3,6,0)*/
559#else /*ifdef DEBUG*/
560# define Assert(cond, exc) \
561 do \
562 { \
563 if (false) \
564 if (!(cond)) \
565 { \
566 } \
567 } \
568 while (false)
569#endif /*ifdef DEBUG*/
570
571
572
599#ifdef DEBUG
600# define AssertNothrow(cond, exc) \
601 do \
602 { \
603 if (DEAL_II_BUILTIN_EXPECT(!(cond), false)) \
604 ::deal_II_exceptions::internals::issue_error_nothrow( \
605 __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
606 } \
607 while (false)
608#else
609# define AssertNothrow(cond, exc) \
610 do \
611 { \
612 if (false) \
613 if (!(cond)) \
614 { \
615 } \
616 } \
617 while (false)
618#endif
619
647#define AssertThrow(cond, exc) \
648 do \
649 { \
650 if (DEAL_II_BUILTIN_EXPECT(!(cond), false)) \
651 ::deal_II_exceptions::internals::issue_error_noreturn( \
652 ::deal_II_exceptions::internals::ExceptionHandling:: \
653 throw_on_exception, \
654 __FILE__, \
655 __LINE__, \
656 __PRETTY_FUNCTION__, \
657 #cond, \
658 #exc, \
659 exc); \
660 } \
661 while (false)
662
663
705#define DEAL_II_NOT_IMPLEMENTED() \
706 ::deal_II_exceptions::internals::do_not_implemented( \
707 __FILE__, __LINE__, __PRETTY_FUNCTION__)
708
709
781#define DEAL_II_ASSERT_UNREACHABLE() \
782 ::deal_II_exceptions::internals::do_unreachable( \
783 __FILE__, \
784 __LINE__, \
785 __PRETTY_FUNCTION__, \
786 "The program has hit a line of code that the programmer " \
787 "marked with the macro DEAL_II_ASSERT_UNREACHABLE() to " \
788 "indicate that the program should never reach this " \
789 "location. You will have to find out (best done in a " \
790 "debugger) why that happened. Typical reasons include " \
791 "passing invalid arguments to functions (for example, if " \
792 "a function takes an 'enum' with two possible values " \
793 "as argument, but you call the function with a third " \
794 "value), or if the programmer of the code that triggered " \
795 "the error believed that a variable can only have " \
796 "specific values, but either that assumption is wrong " \
797 "or the computation of that value is buggy." \
798 "\n\n" \
799 "In those latter conditions, where some internal " \
800 "assumption is not satisfied, there may not be very " \
801 "much you can do if you encounter such an exception, " \
802 "since it indicates an error in deal.II, not in your " \
803 "own program. If that is the situation you encounter, " \
804 "try to come up with " \
805 "the smallest possible program that still demonstrates " \
806 "the error and contact the deal.II mailing lists with it " \
807 "to obtain help.")
808
829#define AssertDimension(dim1, dim2) \
830 Assert(::deal_II_exceptions::internals::compare_for_equality(dim1, \
831 dim2), \
832 ::ExcDimensionMismatch((dim1), (dim2)))
833
842#define AssertIntegerConversion(index1, index2) \
843 Assert(::deal_II_exceptions::internals::compare_for_equality( \
844 index1, index2), \
845 ::ExcInvalidIntegerConversion((index1), (index2)))
846
851#define AssertThrowIntegerConversion(index1, index2) \
852 AssertThrow(::deal_II_exceptions::internals::compare_for_equality( \
853 index1, index2), \
854 ::ExcInvalidIntegerConversion((index1), (index2)))
855
873#define AssertVectorVectorDimension(VEC, DIM1, DIM2) \
874 AssertDimension(VEC.size(), DIM1); \
875 for (const auto &subvector : VEC) \
876 { \
877 (void)subvector; \
878 AssertDimension(subvector.size(), DIM2); \
879 }
880
881
901#define AssertIndexRange(index, range) \
902 Assert( \
903 ::deal_II_exceptions::internals::compare_less_than(index, \
904 range) && \
905 [](const auto ind) { \
906 if constexpr (std::is_unsigned_v<decltype(ind)>) \
907 return true; \
908 else \
909 return (ind >= decltype(ind)(0)); \
910 }(index), \
911 ::ExcIndexRangeType<::internal::argument_type_t<void( \
912 std::common_type_t<decltype(index), decltype(range)>)>>((index), \
913 0, \
914 (range)))
915
932#define AssertIsFinite(number) \
933 Assert(::numbers::is_finite(number), ::ExcNumberNotFinite(number))
934
940#define AssertIsNotUsed(obj) Assert((obj)->used() == false, ExcInternalError())
941
942#ifdef DEAL_II_WITH_MPI
963# define AssertThrowMPI(error_code) \
964 AssertThrow(error_code == MPI_SUCCESS, ::ExcMPI(error_code))
965#else
966# define AssertThrowMPI(error_code) \
967 do \
968 { \
969 } \
970 while (false)
971#endif // DEAL_II_WITH_MPI
972
973#ifdef DEAL_II_TRILINOS_WITH_SEACAS
991# define AssertThrowExodusII(error_code) \
992 AssertThrow(error_code == 0, \
993 ::StandardExceptions::ExcExodusII(error_code));
994#endif // DEAL_II_TRILINOS_WITH_SEACAS
995
996
997#ifdef DEAL_II_WITH_SUNDIALS
1015# define AssertARKode(code) \
1016 Assert(code >= 0, ::SUNDIALS::ExcARKodeError(code))
1017
1035# define AssertKINSOL(code) \
1036 Assert(code >= 0, ::SUNDIALS::ExcKINSOLError(code))
1037
1055# define AssertIDA(code) \
1056 Assert(code >= 0, ::SUNDIALS::ExcIDAError(code))
1057#endif
1058
1059#ifdef DEAL_II_WITH_NETCDF
1078# define AssertThrowNC(code) \
1079 AssertThrow(code == NC_NOERR, ::ExcIO(::nc_strerror(code)))
1080#endif
1081
1082#endif