lua-users home
lua-l archive

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




On Thu, Sep 11, 2014 at 12:41 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
2014-09-11 2:43 GMT+02:00 Rena <hyperhacker@gmail.com>:

> The idiom I've heard and used is that returning (nil, errmsg) is for
> runtime errors, and error() is for programming errors. So e.g.:
> return nil, "Failed to open file: access denied"
> vs:
> error("Invalid argument to method frobulate (expected number)")

The question one should ask is: could one reasonably expect
the calling routine have a nice way of recovering from this error?
If so, return nil,error_obj; otherwise abort.

And assert only tautologies, in order to assist documentation.
It says "If we get here, the following condition is guaranteed
to be true." in a way that inspires confidence.


I do this and it's one of the few error handling-esque patterns that I'm comfortable with. Lua's `assert` function is more flexible, so it takes some discipline to not use it in more ways.

--Andrew