[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Using exceptions in the core
From: |
Jordi Gutiérrez Hermoso |
Subject: |
Re: Using exceptions in the core |
Date: |
Mon, 28 Sep 2015 10:42:41 -0400 |
On Sun, 2015-09-27 at 10:38 +0200, Olaf Till wrote:
> On Sat, Sep 26, 2015 at 12:13:03PM -0400, John W. Eaton wrote:
> > ...
> > But in other cases, more changes are needed, or we have to accept different
> > behavior. For example we currently have things like this
> >
> > std::string val = args(0).string_value ();
> >
> > if (! error_state)
> > {
> > // normal processing...
> > }
> > else
> > error ("foobar: first argument must be a character string");
> >
> > With the change to have the error function throw an exception, the
> > octave_value::string_value extractor will throw an error and so the
> > subsequent more useful message will not appear. I think in these cases, the
> > fix is to write
> >
> > if (! args(0).is_string ())
> > error ("foobar: first argument must be a character string");
> >
> > // normal processing
> >
> > But there are other cases of extracting values that are not as clear and may
> > need more work.
>
> What about providing
>
> #define SOME_MACRO(code, informative_error_message) \
> ...
Death to macros!
In this case it's not so onerous to do
try
{
std::string val = args(0).string_value ();
}
catch (invalid_value& e)
{
error ("foobar: first argument must be a character string");
}
In general, replacing if(! error_state) with try-catch blocks is not
that complicated, but it will require changing a lot of calling sites.
- Jordi G. H.