lua-users home
lua-l archive

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

On Tue, Apr 28, 2015 at 13:19:04 +0300, Konstantin Osipov wrote:
> The first problem is in ambiguity of the second argument. 
> In case there is no error, it's the return value of the function.
> Otherwise, it's a string with a message. 

I tend to use 'ok, result = pcall(xyz, blah)' and then:

if ok then
  ...use result as useful result...
  ...result is the error...

> How to name the second argument? And what if a function returns
> multiple arguments?

Multiple returns is a toughie and I will admit I tend not to use
them in cases where I want to return errors too.  Then again I tend
to code functions which return an ok, result tuple most of the time
and then deal with wrappering them in assert() when I'd rather they
threw an error.

> The solution we adopted is to never treat the second argument
> as an error, but use a separate API to fetch the error.

This is a not-uncommon approach in use in many other languages.  When
I did a database wrapper in another place and time I had something
equivalent to:

if not db_handle:insert(.....) then

> As can be seen from the snippet, we also extended the error
> handling logic to be able to raise and check additional
> attributes of the error, such as code or error number,
> and added an ability to re-throw an already raised error,
> without change.

Did you extend it to actually wrapper xpcall and record error location and
stack trace too?  That might be of use to debuggers.

> It would be nice to see support for these needs in some future
> version of the Lua language. Especially since all the mechanics
> is already there, and we're only talking about making it
> easier to use.

There have been various attempts at using token filters to introduce more
python or C++ like exception handling into the language.  None were widely
enough liked to warrant a language change though.


Daniel Silverstone               
PGP mail accepted and encouraged.            Key Id: 3CCE BABE 206C 3B69