|
Boost Users : |
Subject: Re: [Boost-users] How to make Boost.Format fault tolerant?
From: Leo Cacciari (leo.cacciari_at_[hidden])
Date: 2012-02-11 08:50:57
Il 02/11/2012 08:52 AM, John M. Dlugosz ha scritto:
> On 2/11/2012 1:43 AM, John M. Dlugosz wrote:
>>
>> It seems to suppress exceptions for some things, but a format string
>> like:
>> "Funny format string: 25% off now! %z %x"
>> still throws an exception "boost::bad_format_string: format-string is
>> ill-formed"
>> when exceptions(boost::io::no_error_bits) is used.
>>
>
> The example could be more enlighteningâ¦
>
>
> boost::format my_fmt(const std::string & f_string) {
> using namespace boost::io;
> format fmter(f_string);
> fmter.exceptions( all_error_bits ^ ( too_many_args_bit |
> too_few_args_bit ));
> return fmter;
> }
>
>
> doesn't work for errors in the format string itself.
>
>
> It would be better illustrated along the lines of:
>
> boost::format my_fmt(const std::string & f_string) {
> using namespace boost::io;
> format fmter; // don't give it the string until after the
> exceptions are set
> fmter.exceptions( whatever );
> fmter.parse (f_string);
> return fmter;
> }
You could just wrap the format instance creation in a try/catch which
would eat away the exception and return a static instance of format, one
that ignores al arguments except the line and the file and prints
something like "format error in format string at line L in file F"
Instead of the static instance you could also generate a dynamic one
including somehow informations about the exception itself, but this
could throw again and I advise against it...
LC
-- Leo Cacciari Aliae nationes servitutem pati possunt populi romani est propria libertas
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net