octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using exceptions in the core


From: Rik
Subject: Re: Using exceptions in the core
Date: Fri, 2 Oct 2015 11:39:25 -0700

On 10/02/2015 10:05 AM, address@hidden wrote:
Subject:
Re: Using exceptions in the core
From:
Olaf Till <address@hidden>
Date:
09/30/2015 11:47 PM
To:
"John W. Eaton" <address@hidden>
CC:
Rik <address@hidden>, Octave-Maintainers <address@hidden>
List-Post:
<mailto:address@hidden>
Precedence:
list
MIME-Version:
1.0
References:
<address@hidden> <address@hidden> <address@hidden> <address@hidden> <address@hidden>
In-Reply-To:
<address@hidden>
Message-ID:
<address@hidden>
Content-Type:
multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="V0207lvV8h4k8FAm"
Message:
1

On Wed, Sep 30, 2015 at 04:58:22PM -0400, John W. Eaton wrote:
> On 09/28/2015 10:42 AM, Jordi Gutiérrez Hermoso wrote:
> >On Sun, 2015-09-27 at 10:38 +0200, Olaf Till wrote:
> 
> >>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.
> 
> I think we also want to avoid adding many try-catch blocks if possible.
> 
> In this case, instead of a macro, maybe an in-line function like
> 
>   inline std::string
>   get_string (const octave_value& arg, const std::string& msg = std::string
> ())
>   {
>      std::string retval;
> 
>      try
>        {
>           retval = arg.string_value ();
>        }
>      catch
>        {
>          if (! msg.empty ())
>            error ("%s", msg.c_str ());
>          else
>            throw;
>        }
> 
>      return retval;
>   }
> 
> Then we could just write
> 
>   std::string val = get_string (args(0), "foobar: first argument must be a
> character string");
> 
> and we would (currently) see something like
> 
>   error: octave_base_value::convert_to_str_internal (): wrong type argument
> 'matrix'
>   error: foobar: first argument must be a character string
> 
> It would probably be good to take this opportunity to modify the behavior to
> either omit the wrong-type argument message, or at least clean it up so that
> it doesn't print the "octave_base_value::convert_to_str_internal ()" part.
> 
> In any case, this seems cleaner and clearer than having either the try-catch
> block repeated many times or writing
> 
>   if (! args(0).is_string ())
>     {
>       error ("foobar: first argument must be a character string");
>       return retval;
>     }
> 
>   std::string val = args(0).string_value ();
> 
> I'm not sure whether I would have this as a simple function or make it a
> method in the otave_value class.  I suppose it could just be an overloaded
> version of the octave_value::string_value method.
Overloading the ..._value() methods seems good to me if this way is
gone. But of course in this way you only handle the foreseen cases,
mostly ..._value() conversions, and you have to do it for each
separately ... still it would probably spare a good deal of 'outer'
try-catch blocks ...

As for testing with is_...() before conversion, I'd agree it isn't
very nice.

Olaf

I also like the overloaded _value() method idea.  We could start out small by just overloading the string_value () method since this seems to be one of the most frequent occurrences where the function expected a string, for an option maybe, but numeric input was given.

--Rik

reply via email to

[Prev in Thread] Current Thread [Next in Thread]