]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Throw the exception at the end of ParameterHandler::declare_entry() so as to make...
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Thu, 22 Aug 2013 18:37:49 +0000 (18:37 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Thu, 22 Aug 2013 18:37:49 +0000 (18:37 +0000)
git-svn-id: https://svn.dealii.org/trunk@30430 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/include/deal.II/base/parameter_handler.h
deal.II/source/base/parameter_handler.cc
tests/bits/parameter_handler_1_exception.cc [new file with mode: 0644]
tests/bits/parameter_handler_1_exception/cmp/generic [new file with mode: 0644]
tests/bits/parameter_handler_1_exception/prm [new file with mode: 0644]

index 92868e2f37a11727001808a9434fc9d9536bdb72..80814702202b91bdcc340ae784ce9140a4a459cf 100644 (file)
@@ -1631,8 +1631,14 @@ public:
    * this class is asked to write out all declarations to a stream using
    * the print_parameters() function.
    *
-   * The function generates an exception if the default value doesn't match
-   * the given pattern. An entry can be declared more than once without
+   * The function generates an exception of type ExcValueDoesNotMatchPattern
+   * if the default value doesn't match the given pattern, using the C++
+   * throw mechanism. However, this exception is only generated <i>after</i>
+   * the entry has been created; if you have code where no sensible default
+   * value for a parameter is possible, you can then catch and ignore this
+   * exception.
+   *
+   * @note An entry can be declared more than once without
    * generating an error, for example to override an earlier default value.
    */
   void declare_entry (const std::string           &entry,
index 78adb441beb283797f9a537c6e6e8db98bffd2b8..e420fc5d42daa1860901b12f5f4b204241b99867 100644 (file)
@@ -1579,14 +1579,6 @@ ParameterHandler::declare_entry (const std::string           &entry,
                                  const Patterns::PatternBase &pattern,
                                  const std::string           &documentation)
 {
-  Assert (pattern.match (default_value),
-         ExcMessage (std::string("The default value <") +
-                     default_value +
-                     "> for the entry <" +
-                     entry +
-                     "> does not match the given pattern " +
-                     pattern.description()));
-  
   entries->put (get_current_full_path(entry) + path_separator + "value",
                 default_value);
   entries->put (get_current_full_path(entry) + path_separator + "default_value",
@@ -1612,6 +1604,10 @@ ParameterHandler::declare_entry (const std::string           &entry,
   entries->put (get_current_full_path(entry) + path_separator +
                 "pattern_description",
                 patterns.back()->description());
+
+  // as documented, do the default value checking at the very end
+  AssertThrow (pattern.match (default_value),
+               ExcValueDoesNotMatchPattern (default_value, pattern.description()));
 }
 
 
diff --git a/tests/bits/parameter_handler_1_exception.cc b/tests/bits/parameter_handler_1_exception.cc
new file mode 100644 (file)
index 0000000..75b17ff
--- /dev/null
@@ -0,0 +1,58 @@
+// ---------------------------------------------------------------------
+// $Id$
+//
+// Copyright (C) 2002 - 2013 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+
+// ParameterHandler::declare_entry throws an exception if the default
+// value of an entry doesn't match the pattern; but it should still
+// yield a properly declared entry
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+void check (const char *p)
+{
+  ParameterHandler prm;
+  try
+    {
+      prm.declare_entry ("test_1", "abc",
+                        Patterns::List(Patterns::Integer(-1,1),2,3));
+    }
+  catch (const ParameterHandler::ExcValueDoesNotMatchPattern &)
+    {
+      deallog << "Exception caught as expected." << std::endl;
+    }
+
+  std::ifstream in(p);
+  prm.read_input (in);
+
+  deallog << "test_1=" << prm.get ("test_1") << std::endl;
+}
+
+
+int main ()
+{
+  std::ofstream logfile("parameter_handler_1_exception/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  check ("parameter_handler_1_exception/prm");
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_1_exception/cmp/generic b/tests/bits/parameter_handler_1_exception/cmp/generic
new file mode 100644 (file)
index 0000000..a0a3091
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL::Exception caught as expected.
+DEAL::test_1=1,0,1
diff --git a/tests/bits/parameter_handler_1_exception/prm b/tests/bits/parameter_handler_1_exception/prm
new file mode 100644 (file)
index 0000000..4ed6296
--- /dev/null
@@ -0,0 +1 @@
+set test_1 = 1,0,1

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.