From ba3b5eb493cddc372aded9754b0f1b6e74d61453 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Wed, 9 Jan 2002 16:51:27 +0000 Subject: [PATCH] Further improve the working of the exception mechanism. git-svn-id: https://svn.dealii.org/trunk@5370 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/exceptions.h | 69 ++++++++++++++++++++++---- deal.II/base/source/exceptions.cc | 3 ++ 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/deal.II/base/include/base/exceptions.h b/deal.II/base/include/base/exceptions.h index e0c21fbc88..3fade7e8f5 100644 --- a/deal.II/base/include/base/exceptions.h +++ b/deal.II/base/include/base/exceptions.h @@ -180,6 +180,12 @@ * not terminate the program to allow that the thrown exception * propagates to some place where its message can be displayed. * + * Since it is common that one failed assertion leads to a whole + * chain of others, we only ever print the very first message. If the + * program is then aborted, that is no problem. If it is not (since a + * C++ exception is active), only the first is displayed and a + * message about suppressed follow-up messages is shown. + * * * @sect2{Use of run-time exceptions} * @@ -394,6 +400,28 @@ class ExceptionBase : public std::exception * Name of the exception and call sequence. */ const char *exc; + + /** + * Number of exceptions dealt + * with so far. Zero at program + * start. Messages are only + * displayed if the value is + * zero. + */ + static unsigned int n_treated_exceptions; + + /** + * Make this function a friend + * since it needs access to the + * @p{n_treated_exceptions} + * variable. + */ + template friend void __IssueError_Assert (const char *file, + int line, + const char *function, + const char *cond, + const char *exc_name, + exc e); }; @@ -413,16 +441,37 @@ void __IssueError_Assert (const char *file, const char *exc_name, exc e) { - // Fill the fields of the exception object + // fill the fields of the exception object e.SetFields (file, line, function, cond, exc_name); - std::cerr << "--------------------------------------------------------" - << std::endl; - // print out general data - e.PrintExcData (std::cerr); - // print out exception specific data - e.PrintInfo (std::cerr); - std::cerr << "--------------------------------------------------------" - << std::endl; + + // if no other exception has been + // displayed before, show this one + if (ExceptionBase::n_treated_exceptions == 0) + { + std::cerr << "--------------------------------------------------------" + << std::endl; + // print out general data + e.PrintExcData (std::cerr); + // print out exception specific data + e.PrintInfo (std::cerr); + std::cerr << "--------------------------------------------------------" + << std::endl; + } + else + { + // if this is the first + // follow-up message, display a + // message that further + // exceptions are suppressed + if (ExceptionBase::n_treated_exceptions == 1) + std::cerr << "******** More assertions fail but messages are suppressed! ********" + << std::endl; + }; + + // increase number of treated + // exceptions by one + ExceptionBase::n_treated_exceptions++; + // abort the program now since // something has gone horribly @@ -435,7 +484,7 @@ void __IssueError_Assert (const char *file, // see the original exception. in // that case indicate that the // program is not aborted due to - // this reason + // this reason. if (std::uncaught_exception() == true) std::cerr << "******** Program is not aborted since another exception is active! ********" << std::endl; diff --git a/deal.II/base/source/exceptions.cc b/deal.II/base/source/exceptions.cc index 0f8eca90a2..18e293694f 100644 --- a/deal.II/base/source/exceptions.cc +++ b/deal.II/base/source/exceptions.cc @@ -25,6 +25,9 @@ #endif +unsigned int ExceptionBase::n_treated_exceptions; + + ExceptionBase::ExceptionBase () : file(""), line(0), function(""), cond(""), exc("") {}; -- 2.39.5