]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Bugfix: Improve dealii::ExceptionBase
authormaier <maier@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 23 Oct 2013 09:15:40 +0000 (09:15 +0000)
committermaier <maier@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 23 Oct 2013 09:15:40 +0000 (09:15 +0000)
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

deal.II/include/deal.II/base/exceptions.h
deal.II/source/base/exceptions.cc

index 3ac055a0ac410ede2111aa2ad6e34f746d66a5df..fceafdd46c6e13c7887355712dfe585d51034661 100644 (file)
@@ -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();
 };
 
 
index 81f8b3075eda3ae09504581436fe2e7ad0a9f367..b71a75d98a670b7fe76b97e7a038a9bac3626455 100644 (file)
@@ -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<std::runtime_error &>(*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<std::runtime_error &>(*this) = std::runtime_error(converter.str());
+}
+
+
+
 namespace deal_II_exceptions
 {
   namespace internals

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.