lua-users home
lua-l archive

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

Glenn Maynard wrote:
> I see this as analogous to C++ errors and exceptions.  Good C++ code,
> in my opinion, doesn't throw an exception for a normal error--an error
> that happens in the normal use of a program, that the caller of the
> function will always want to handle.  It does throw an exception for
> exceptional events, that the caller does not.  You throw an exception
> if, while trying to open a file, a memory allocation for a buffer fails;
> you don't throw an exception if the file simply doesn't exist.
> I see lua_error() as analogous to throwing an exception, and "return
> nil, error" as returning an error.

The criteria I developed for my Lua workshop presentation on exceptions
[1] is to raise an exception when it's unlikely that the immediate
caller of the function can handle the error.  Exceptions become a good
value when you are communicating errors at least two stack frames up.
So for example, a string match function should signal a mismatch by
return value, while a file open function should raise an exception on
any error ( doesn't do this).

There are a number of issues working to discourage better use of
exceptions in Lua libraries:  the pcall/coroutine problem, lack of a
native try-except construct, and the unfortunate practice of throwing
strings (i.e. no error hierarchy to assist catching specific exceptions
or classes of exceptions).