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: John W. Eaton
Subject: Re: Using exceptions in the core
Date: Wed, 30 Sep 2015 16:58:22 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0

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.

jwe




reply via email to

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