lua-users home
lua-l archive

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


On Tue, May 31, 2016 at 2:11 PM, steve donovan <steve.j.donovan@gmail.com> wrote:

> I tend to side with Jonne here - exceptions are overused.

Unfortunately, in Lua and some other garbage-collected languages, exceptions (errors in Lua) and a lack of deterministic destruction (finalization) combine to produce a very nasty problem.

The example given by Alysson demonstrates the problem. The programmer desires to catch the exception to close the file handle (or whatever other expensive resource it might be). Why does the programmer desire to do so? Because, without being closed by the programmer, it will only be closed when the garbage collector runs its finalizer. This is unpredictable and definitely not how one should treat expensive resources (I remember vividly a short piece of Lua code that brought a meaty DB server to its knees by accumulating thousands of open DB connections because an error prevented it from closing them normally, and GC was somehow taking a nap). Hence the desire to handle exceptions explicitly. But the pcall mechanism is very low level, hence the desire to have a higher-level try/catch/finally abstraction.

Fundamentally, however, the flaw in the language is not its lack of a higher-level exception-handling abstraction, but its inability to finalize expensive resources deterministically. In C++, destructors do that, and, indeed, in C++ one can just get by with a single exception handler "that prints error messages". In Lua (today) this is not generally feasible.

Cheers,
V.