deal.II version 9.7.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
exception_macros.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2012 - 2025 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_exception_macros_h
16#define dealii_exception_macros_h
17
18#include <deal.II/base/config.h>
19
20
21/**********************************************************************
22 * Preprocessor definitions in support of declaring exception classes.
23 *
24 * These make reference to classes and functions that are declared in
25 * exceptions.h. This is ok as far as concerning preprocessor defines
26 * is concerned, but in order to use the material below, you also need
27 * to have `#include <deal.II/base/exceptions.h>`.
28 */
29#ifndef DOXYGEN
30
47# define DeclException0(Exception0) \
48 class Exception0 : public ::ExceptionBase \
49 {}
50
51
71# define DeclExceptionMsg(Exception, defaulttext) \
72 class Exception : public ::ExceptionBase \
73 { \
74 public: \
75 Exception(const std::string &msg = defaulttext) \
76 : arg(msg) \
77 {} \
78 virtual ~Exception() noexcept \
79 {} \
80 virtual void \
81 print_info(std::ostream &out) const override \
82 { \
83 out << " " << arg << std::endl; \
84 } \
85 \
86 private: \
87 const std::string arg; \
88 }
89
107# define DeclException1(Exception1, type1, outsequence) \
108 class Exception1 : public ::ExceptionBase \
109 { \
110 public: \
111 Exception1(type1 const &a1) \
112 : arg1(a1) \
113 {} \
114 virtual ~Exception1() noexcept \
115 {} \
116 virtual void \
117 print_info(std::ostream &out) const override \
118 { \
119 out << " " outsequence << std::endl; \
120 } \
121 \
122 private: \
123 type1 const arg1; \
124 }
125
126
144# define DeclException2(Exception2, type1, type2, outsequence) \
145 class Exception2 : public ::ExceptionBase \
146 { \
147 public: \
148 Exception2(type1 const &a1, type2 const &a2) \
149 : arg1(a1) \
150 , arg2(a2) \
151 {} \
152 virtual ~Exception2() noexcept \
153 {} \
154 virtual void \
155 print_info(std::ostream &out) const override \
156 { \
157 out << " " outsequence << std::endl; \
158 } \
159 \
160 private: \
161 type1 const arg1; \
162 type2 const arg2; \
163 }
164
165
183# define DeclException3(Exception3, type1, type2, type3, outsequence) \
184 class Exception3 : public ::ExceptionBase \
185 { \
186 public: \
187 Exception3(type1 const &a1, type2 const &a2, type3 const &a3) \
188 : arg1(a1) \
189 , arg2(a2) \
190 , arg3(a3) \
191 {} \
192 virtual ~Exception3() noexcept \
193 {} \
194 virtual void \
195 print_info(std::ostream &out) const override \
196 { \
197 out << " " outsequence << std::endl; \
198 } \
199 \
200 private: \
201 type1 const arg1; \
202 type2 const arg2; \
203 type3 const arg3; \
204 }
205
206
224# define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
225 class Exception4 : public ::ExceptionBase \
226 { \
227 public: \
228 Exception4(type1 const &a1, \
229 type2 const &a2, \
230 type3 const &a3, \
231 type4 const &a4) \
232 : arg1(a1) \
233 , arg2(a2) \
234 , arg3(a3) \
235 , arg4(a4) \
236 {} \
237 virtual ~Exception4() noexcept \
238 {} \
239 virtual void \
240 print_info(std::ostream &out) const override \
241 { \
242 out << " " outsequence << std::endl; \
243 } \
244 \
245 private: \
246 type1 const arg1; \
247 type2 const arg2; \
248 type3 const arg3; \
249 type4 const arg4; \
250 }
251
252
270# define DeclException5( \
271 Exception5, type1, type2, type3, type4, type5, outsequence) \
272 class Exception5 : public ::ExceptionBase \
273 { \
274 public: \
275 Exception5(type1 const &a1, \
276 type2 const &a2, \
277 type3 const &a3, \
278 type4 const &a4, \
279 type5 const &a5) \
280 : arg1(a1) \
281 , arg2(a2) \
282 , arg3(a3) \
283 , arg4(a4) \
284 , arg5(a5) \
285 {} \
286 virtual ~Exception5() noexcept \
287 {} \
288 virtual void \
289 print_info(std::ostream &out) const override \
290 { \
291 out << " " outsequence << std::endl; \
292 } \
293 \
294 private: \
295 type1 const arg1; \
296 type2 const arg2; \
297 type3 const arg3; \
298 type4 const arg4; \
299 type5 const arg5; \
300 }
301
302#else /*ifndef DOXYGEN*/
303
304// Dummy definitions for doxygen:
305
322# define DeclException0(Exception0) \
323 \
324 static ::ExceptionBase &Exception0()
325
345# define DeclExceptionMsg(Exception, defaulttext) \
346 \
347 \
348 static ::ExceptionBase &Exception()
349
367# define DeclException1(Exception1, type1, outsequence) \
368 \
369 \
370 static ::ExceptionBase &Exception1(type1 arg1)
371
372
390# define DeclException2(Exception2, type1, type2, outsequence) \
391 \
392 \
393 static ::ExceptionBase &Exception2(type1 arg1, type2 arg2)
394
395
413# define DeclException3(Exception3, type1, type2, type3, outsequence) \
414 \
415 \
416 static ::ExceptionBase &Exception3(type1 arg1, type2 arg2, type3 arg3)
417
418
436# define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
437 \
438 \
439 static ::ExceptionBase &Exception4(type1 arg1, \
440 type2 arg2, \
441 type3 arg3, \
442 type4 arg4)
443
444
462# define DeclException5( \
463 Exception5, type1, type2, type3, type4, type5, outsequence) \
464 \
465 \
466 static ::ExceptionBase &Exception5( \
467 type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)
468
469#endif /*ifndef DOXYGEN*/
470
471
472
473/**********************************************************************
474 * Preprocessor definitions in support of assertions.
475 *
476 * These make reference to classes and functions that are declared in
477 * exceptions.h. This is ok as far as concerning preprocessor defines
478 * is concerned, but in order to use the material below, you also need
479 * to have `#include <deal.II/base/exceptions.h>`.
480 */
481
486#ifdef DEAL_II_HAVE_BUILTIN_EXPECT
487# define DEAL_II_BUILTIN_EXPECT(a, b) __builtin_expect((a), (b))
488#else
489# define DEAL_II_BUILTIN_EXPECT(a, b) (a)
490#endif
491
492
518#ifdef DEBUG
519# if DEAL_II_KOKKOS_VERSION_GTE(3, 6, 0)
520# define Assert(cond, exc) \
521 do \
522 { \
523 KOKKOS_IF_ON_HOST(({ \
524 if (DEAL_II_BUILTIN_EXPECT(!(cond), false)) \
525 ::deal_II_exceptions::internals::issue_error_noreturn( \
526 ::deal_II_exceptions::internals::ExceptionHandling:: \
527 abort_or_throw_on_exception, \
528 __FILE__, \
529 __LINE__, \
530 __PRETTY_FUNCTION__, \
531 #cond, \
532 #exc, \
533 exc); \
534 })) \
535 KOKKOS_IF_ON_DEVICE(({ \
536 if (!(cond)) \
537 Kokkos::abort(#cond); \
538 })) \
539 } \
540 while (false)
541# else /*if DEAL_II_KOKKOS_VERSION_GTE(3,6,0)*/
542# ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
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# else /*#ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
559# define Assert(cond, exc) \
560 do \
561 { \
562 if (!(cond)) \
563 Kokkos::abort(#cond); \
564 } \
565 while (false)
566# endif /*ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
567# endif /*KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
568#else /*ifdef DEBUG*/
569# define Assert(cond, exc) \
570 do \
571 { \
572 if (false) \
573 if (!(cond)) \
574 { \
575 } \
576 } \
577 while (false)
578#endif /*ifdef DEBUG*/
579
580
581
608#ifdef DEBUG
609# define AssertNothrow(cond, exc) \
610 do \
611 { \
612 if (DEAL_II_BUILTIN_EXPECT(!(cond), false)) \
613 ::deal_II_exceptions::internals::issue_error_nothrow( \
614 __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
615 } \
616 while (false)
617#else
618# define AssertNothrow(cond, exc) \
619 do \
620 { \
621 if (false) \
622 if (!(cond)) \
623 { \
624 } \
625 } \
626 while (false)
627#endif
628
656#define AssertThrow(cond, exc) \
657 do \
658 { \
659 if (DEAL_II_BUILTIN_EXPECT(!(cond), false)) \
660 ::deal_II_exceptions::internals::issue_error_noreturn( \
661 ::deal_II_exceptions::internals::ExceptionHandling:: \
662 throw_on_exception, \
663 __FILE__, \
664 __LINE__, \
665 __PRETTY_FUNCTION__, \
666 #cond, \
667 #exc, \
668 exc); \
669 } \
670 while (false)
671
672
714#define DEAL_II_NOT_IMPLEMENTED() \
715 ::deal_II_exceptions::internals::issue_error_noreturn( \
716 ::deal_II_exceptions::internals::ExceptionHandling:: \
717 abort_or_throw_on_exception, \
718 __FILE__, \
719 __LINE__, \
720 __PRETTY_FUNCTION__, \
721 nullptr, \
722 nullptr, \
723 ::StandardExceptions::ExcNotImplemented())
724
725
797#define DEAL_II_ASSERT_UNREACHABLE() \
798 ::deal_II_exceptions::internals::issue_error_noreturn( \
799 ::deal_II_exceptions::internals::ExceptionHandling:: \
800 abort_or_throw_on_exception, \
801 __FILE__, \
802 __LINE__, \
803 __PRETTY_FUNCTION__, \
804 nullptr, \
805 nullptr, \
806 ::StandardExceptions::ExcMessage( \
807 "The program has hit a line of code that the programmer " \
808 "marked with the macro DEAL_II_ASSERT_UNREACHABLE() to " \
809 "indicate that the program should never reach this " \
810 "location. You will have to find out (best done in a " \
811 "debugger) why that happened. Typical reasons include " \
812 "passing invalid arguments to functions (for example, if " \
813 "a function takes an 'enum' with two possible values " \
814 "as argument, but you call the function with a third " \
815 "value), or if the programmer of the code that triggered " \
816 "the error believed that a variable can only have " \
817 "specific values, but either that assumption is wrong " \
818 "or the computation of that value is buggy." \
819 "\n\n" \
820 "In those latter conditions, where some internal " \
821 "assumption is not satisfied, there may not be very " \
822 "much you can do if you encounter such an exception, " \
823 "since it indicates an error in deal.II, not in your " \
824 "own program. If that is the situation you encounter, " \
825 "try to come up with " \
826 "the smallest possible program that still demonstrates " \
827 "the error and contact the deal.II mailing lists with it " \
828 "to obtain help."))
829
830
851#define AssertDimension(dim1, dim2) \
852 Assert(::deal_II_exceptions::internals::compare_for_equality(dim1, \
853 dim2), \
854 ::ExcDimensionMismatch((dim1), (dim2)))
855
864#define AssertIntegerConversion(index1, index2) \
865 Assert(::deal_II_exceptions::internals::compare_for_equality( \
866 index1, index2), \
867 ::ExcInvalidIntegerConversion((index1), (index2)))
868
873#define AssertThrowIntegerConversion(index1, index2) \
874 AssertThrow(::deal_II_exceptions::internals::compare_for_equality( \
875 index1, index2), \
876 ::ExcInvalidIntegerConversion((index1), (index2)))
877
895#define AssertVectorVectorDimension(VEC, DIM1, DIM2) \
896 AssertDimension(VEC.size(), DIM1); \
897 for (const auto &subvector : VEC) \
898 { \
899 (void)subvector; \
900 AssertDimension(subvector.size(), DIM2); \
901 }
902
903
923#define AssertIndexRange(index, range) \
924 Assert(::deal_II_exceptions::internals::compare_less_than(index, \
925 range), \
926 ::ExcIndexRangeType<::internal::argument_type_t<void( \
927 std::common_type_t<decltype(index), decltype(range)>)>>((index), \
928 0, \
929 (range)))
930
950#define AssertIsFinite(number) \
951 Assert(::numbers::is_finite(number), \
952 ::ExcNumberNotFinite(std::complex<double>(number)))
953
959#define AssertIsNotUsed(obj) Assert((obj)->used() == false, ExcInternalError())
960
961#ifdef DEAL_II_WITH_MPI
982# define AssertThrowMPI(error_code) \
983 AssertThrow(error_code == MPI_SUCCESS, ::ExcMPI(error_code))
984#else
985# define AssertThrowMPI(error_code) \
986 do \
987 { \
988 } \
989 while (false)
990#endif // DEAL_II_WITH_MPI
991
992#ifdef DEAL_II_TRILINOS_WITH_SEACAS
1010# define AssertThrowExodusII(error_code) \
1011 AssertThrow(error_code == 0, \
1012 ::StandardExceptions::ExcExodusII(error_code));
1013#endif // DEAL_II_TRILINOS_WITH_SEACAS
1014
1015
1016#ifdef DEAL_II_WITH_SUNDIALS
1034# define AssertARKode(code) Assert(code >= 0, ExcARKodeError(code))
1035
1053# define AssertKINSOL(code) Assert(code >= 0, ExcKINSOLError(code))
1054
1072# define AssertIDA(code) Assert(code >= 0, ExcIDAError(code))
1073#endif
1074
1075#endif