|
> In fact I notice that io.open in particular *does* appear to be returning> some type of error code, though the manual doesn't mention this. (At leastActually:
> not in the description of io.open, but maybe somewhere?)
Lua 5.1, section 5.7:
Unless otherwise stated, all I/O functions return nil on failure
(plus an error message as a second result and a system-dependent
error code as a third result) and some value different from nil on
success.
Lua 5.2, section 6.8:
Unless otherwise stated, all I/O functions return nil on failure
(plus an error message as a second result and a system-dependent
error code as a third result) and some value different from nil on
success.
The value comes from errno, which is part of ANSI C. The actual text
comes from the Standard C function strerror().
False, if Windows claims to support ANSI C---errno.h is *part* of astandard implementation of C. Now, Windows might not support much beyond
EDOM and ERANGE (I see nothing about EILSEQ in the ANSI C standard) but it
is there. And strerror() should return a human readable string given an
error message.
Well, aside from EDOM and ERANGE, what else to include? My own errno
module [1] uses a ton of #ifdef's (and yes, it's heavily Unix influenced,
but then again, I primarily use Unix). And even though I define a large
number of errors, I tend to only use a few:
EINTR - a system call was interrupted
ETIMEDOUT - an operation timed out
and sometimes EINVAL (invalid paramter to a system call). And that's
about it. Most of the time, as long as I can get a human readable version,
I'm happy.
You can do that now with error().
> I've often also felt like Lua could stand to have a standard "warning"
> function. By default warning() might be something like:
> function warning(msg, ...) io.stderr:write(msg:format(...) .. '\n') end
> but the application could redefine it to send the messages elsewhere or do
> nothing.
So I asked one of my college instructors what to do. His reply, as
distateful as it seems, can't really be argued with: If you don't know how
to handle the error, then don't check for the error.