From: Wolfgang Bangerth Date: Thu, 22 Aug 2013 18:37:49 +0000 (+0000) Subject: Throw the exception at the end of ParameterHandler::declare_entry() so as to make... X-Git-Tag: v8.1.0~997 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5b00e983623d02a6615bdfe7a53b368eb8f19225;p=dealii.git Throw the exception at the end of ParameterHandler::declare_entry() so as to make sure that it can be caught, but that the entry nevertheless has been allocated properly. git-svn-id: https://svn.dealii.org/trunk@30430 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/include/deal.II/base/parameter_handler.h b/deal.II/include/deal.II/base/parameter_handler.h index 92868e2f37..8081470220 100644 --- a/deal.II/include/deal.II/base/parameter_handler.h +++ b/deal.II/include/deal.II/base/parameter_handler.h @@ -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 after + * 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, diff --git a/deal.II/source/base/parameter_handler.cc b/deal.II/source/base/parameter_handler.cc index 78adb441be..e420fc5d42 100644 --- a/deal.II/source/base/parameter_handler.cc +++ b/deal.II/source/base/parameter_handler.cc @@ -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 index 0000000000..75b17ffc28 --- /dev/null +++ b/tests/bits/parameter_handler_1_exception.cc @@ -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 +#include +#include + +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 index 0000000000..a0a30917af --- /dev/null +++ b/tests/bits/parameter_handler_1_exception/cmp/generic @@ -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 index 0000000000..4ed62961ce --- /dev/null +++ b/tests/bits/parameter_handler_1_exception/prm @@ -0,0 +1 @@ +set test_1 = 1,0,1