lua-users home
lua-l archive

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


I'm wondering if anybody has had any experience modifying how Lua
handles errors in scripts.  I just finished working on a project using a
different scripting language, and one of the main differences (compared
to Lua) was how it handled errors.  Instead of throwing an exception
when an error was encountered, the script would continue execution,
using nil as the value of the expression that contained the error.  (We
still halted execution of the script at the point of the error and
displayed an error screen, but execution of the script would continue
after the error screen had been bypassed.)

This ended up being quite useful in the context of our application (a
console game).  Scripters that had introduced a typo in one of their
function names would be notified of the error, but the rest of their
code would still be executed.  As game script is often just a sequence
of function calls to engine functions that have been exposed to Lua,
it's usually better to execute all but the mis-spelt function call(s)
instead of executing the function calls up to the error, and ignoring
the rest.

We've thought of a couple of solutions that could fix this issue without
modifying the Lua code:
1. Assign a metatable to 'nil' with a __call metamethod that generates
an error screen, but returns nil.
2. Assign a metatable to _G with a __index metamethod that generates an
error screen, returns a function that returns nil.

Either of these solutions would handle the case of misspelt function
names.  They each have their own issues though.  In the case of (1), I'm
not sure if it's really a good idea to give 'nil' a metatable (some
posts in the archive indicate that it might not be).  In the case of
(2), the expression "foo + 5" would generate an error along the lines of
"trying to add a function to a number" if foo was undefined, which isn't
very intuitive.

Has anybody had any success trying to do something similar?  Or is
continuing script execution after errors just a bad idea when working
with Lua (or in general, despite our fairly positive experience with

Jasmin Patry