From 1e71f438a185fe37f85ab7faa34313b90bbfec27 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 27 Aug 2016 18:46:16 +0200 Subject: [PATCH] Gracefully handle non existent options --- xs/src/libslic3r/Config.cpp | 9 ++++----- xs/src/libslic3r/Config.hpp | 3 +++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/xs/src/libslic3r/Config.cpp b/xs/src/libslic3r/Config.cpp index 39fb4bbe7c..372b7b1b03 100644 --- a/xs/src/libslic3r/Config.cpp +++ b/xs/src/libslic3r/Config.cpp @@ -125,7 +125,7 @@ ConfigBase::serialize(const t_config_option_key &opt_key) const { bool ConfigBase::set_deserialize(const t_config_option_key &opt_key, std::string str, bool append) { const ConfigOptionDef* optdef = this->def->get(opt_key); - if (optdef == NULL) throw std::runtime_error("Calling set_deserialize() on unknown option"); + if (optdef == NULL) throw UnknownOptionException(); if (!optdef->shortcut.empty()) { for (std::vector::const_iterator it = optdef->shortcut.begin(); it != optdef->shortcut.end(); ++it) { if (!this->set_deserialize(*it, str)) return false; @@ -203,11 +203,10 @@ ConfigBase::load(const std::string &file) pt::ptree tree; pt::read_ini(file, tree); BOOST_FOREACH(const pt::ptree::value_type &v, tree) { - try { + try { this->set_deserialize(v.first.c_str(), v.second.get_value().c_str()); - } catch (std::runtime_error& e) { - // skip over errors in the config file but print a warning. - std::cerr << "Caught exception at option " << v.first.c_str() << ".\n"; + } catch (UnknownOptionException &e) { + // ignore } } } diff --git a/xs/src/libslic3r/Config.hpp b/xs/src/libslic3r/Config.hpp index 7892e3358e..ff9762a2e6 100644 --- a/xs/src/libslic3r/Config.hpp +++ b/xs/src/libslic3r/Config.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -633,6 +634,8 @@ class StaticConfig : public virtual ConfigBase void set_defaults(); }; +class UnknownOptionException : public std::exception {}; + } #endif