From: maier Date: Wed, 23 Oct 2013 09:15:40 +0000 (+0000) Subject: Bugfix: Improve dealii::ExceptionBase X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b3a916cf57ee6900f97dfa3bad02953c14be9b35;p=dealii-svn.git Bugfix: Improve dealii::ExceptionBase Also provide a minimalistic error message if an exception derived from dealii::ExceptionBase is just merely thrown (without the use of Assert or AssertThrow). git-svn-id: https://svn.dealii.org/trunk@31398 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/include/deal.II/base/exceptions.h b/deal.II/include/deal.II/base/exceptions.h index 3ac055a0ac..fceafdd46c 100644 --- a/deal.II/include/deal.II/base/exceptions.h +++ b/deal.II/include/deal.II/base/exceptions.h @@ -64,7 +64,7 @@ public: /** * Set the file name and line of where the exception appeared as * well as the violated condition and the name of the exception as - * a char pointer. + * a char pointer. This function also populates the stacktrace. */ void set_fields (const char *file, const int line, @@ -131,6 +131,14 @@ protected: * variable. Zero if the system does not support stack traces. */ int n_stacktrace_frames; + +private: + /** + * Internal function that generates the c_string that gets printed by + * exception::what(). Called by the ExceptionBase constructor and + * set_fields. + */ + void generate_message(); }; diff --git a/deal.II/source/base/exceptions.cc b/deal.II/source/base/exceptions.cc index 81f8b3075e..b71a75d98a 100644 --- a/deal.II/source/base/exceptions.cc +++ b/deal.II/source/base/exceptions.cc @@ -70,7 +70,10 @@ ExceptionBase::ExceptionBase () exc(""), stacktrace (0), n_stacktrace_frames (0) -{} +{ + // Construct a minimalistic error message: + generate_message(); +} @@ -111,39 +114,22 @@ void ExceptionBase::set_fields (const char *f, cond = c; exc = e; - // if the system supports this, get a stacktrace how we got here + // If the system supports this, get a stacktrace how we got here: + + if (stacktrace != 0) + { + free (stacktrace); + stacktrace = 0; + } + #ifdef HAVE_GLIBC_STACKTRACE void *array[25]; n_stacktrace_frames = backtrace(array, 25); stacktrace = backtrace_symbols(array, n_stacktrace_frames); #endif - // build up a string with the error message... - - std::ostringstream converter; - - converter << std::endl - << "--------------------------------------------------------" - << std::endl; - // print out general data - print_exc_data (converter); - // print out exception specific data - print_info (converter); - print_stack_trace (converter); - - if (!deal_II_exceptions::additional_assert_output.empty()) - { - converter << "--------------------------------------------------------" - << std::endl - << deal_II_exceptions::additional_assert_output - << std::endl; - } - - converter << "--------------------------------------------------------" - << std::endl; - - // ... and setup the final error message with it: - static_cast(*this) = std::runtime_error(converter.str()); + // And finally populate the underlying std::runtime_error: + generate_message(); } @@ -276,6 +262,39 @@ void ExceptionBase::print_stack_trace (std::ostream &out) const +void ExceptionBase::generate_message () +{ + // build up a string with the error message... + + std::ostringstream converter; + + converter << std::endl + << "--------------------------------------------------------" + << std::endl; + + // print out general data + print_exc_data (converter); + // print out exception specific data + print_info (converter); + print_stack_trace (converter); + + if (!deal_II_exceptions::additional_assert_output.empty()) + { + converter << "--------------------------------------------------------" + << std::endl + << deal_II_exceptions::additional_assert_output + << std::endl; + } + + converter << "--------------------------------------------------------" + << std::endl; + + // ... and set up std::runtime_error with it: + static_cast(*this) = std::runtime_error(converter.str()); +} + + + namespace deal_II_exceptions { namespace internals