From b762e3faec66e4401eab9c1aa01ea9407dcaa5c7 Mon Sep 17 00:00:00 2001 From: eamanu Date: Mon, 24 Jun 2019 19:11:53 -0300 Subject: [PATCH] Create a exception to thow when Options not exists When the user try to get a option that not exist, cxxopt library throw a std::domain_error, that make a segfaults. See #185. On this PR I create a new Exception class from OptionParseException that is throw when this situation occur. This avoid the SegFaults error. --- include/cxxopts.hpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index 1381ab3..f19b2cc 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -394,6 +394,15 @@ namespace cxxopts } }; + class option_value_not_exists_exception : public OptionParseException + { + public: + option_value_not_exists_exception() + : OptionParseException("Option value does not exist") + { + } + }; + class missing_argument_exception : public OptionParseException { public: @@ -1068,7 +1077,7 @@ namespace cxxopts as() const { if (m_value == nullptr) { - throw std::domain_error("No value"); + throw option_value_not_exists_exception(); } #ifdef CXXOPTS_NO_RTTI