* 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,
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",
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()));
}
--- /dev/null
+// ---------------------------------------------------------------------
+// $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;
+}