From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-09-15 10:19:01


Hi Llew,

> The version of Boost.program_options I am using throws lots of informative
> exceptions to indicate precisely what has gone wrong.
>
> The exception is where an incorrect argument to an option has been
> supplied
>
> e.g.
> --some_arg "abc"
> where
> --some_arg 0.6
> is expected.
>
> The error message is annoyingly non-descript and it can be difficult to
> figure out which particular optional argument has been mis-specified.

I agree with you.

> This imprecision is because when program_options::validation_error is
> being thrown, it is not clear which option is being parsed.
>
> Could I suggest that this particular exception is caught in:
> void store(const parsed_options& options, variables_map& xm,
> bool utf8)
> and the option context added back in (and the same exception type
> re-thrown):

> catch ( validation_error& e )
> {
> throw validation_error("[--" + name + "]: " + e.what());
> }
>
> }
>
> ...
> }
>
> What do you think?

Personally, I would prefer something like:

   catch(validation_error& e)
   {
        e.set_option_name(name);
        throw;
   }

However, that would require to format the string inside the 'what' method,
something like:

class validation_error
{
   std::string m_option_name;
   std::string m_error_message;
   const char* what() const
   {
       m_error_message = logic_error::what();
       if (!m_option_name.empty())
        m_error_message = "--" + m_option_name + " : " + m_error_message;
       return m_error_message.c_str();
   }
};

I've used this approach once, but I'm not sure if has any hidden problems.
I'd appreciate if some exception handling expert could comment on this.

Thanks,
Volodya