[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [proposal] debug.geterrfunc to get current error function
- From: David Manura <dm.lua@...>
- Date: Thu, 23 Feb 2012 21:20:42 -0500
On Sun, Mar 13, 2011 at 8:38 AM, GrayFace <sergroj@mail.ru> wrote:
> [...] I need a way to get the current error function, that is, the one passed
> to the last lua_pcall
> [...] I think this "geterrfunc" should be a part of standard debug library
> and also part of C API.
I made a similar comment in my complaint about how tracebacks work
[1]. Part of the complaint boils down to the failed expectation that
any code of the form
f(...)
can be safely (with zero side-effects) refactored into
local ok, err = pcall(f, ...)
if not ok then error(err) end
Here's a typical use case:
function g(callback)
local file = io.open('data.txt') -- allocate resource
. . .
local ok, err = pcall(callback) -- invoke user callback
. . .
file:close() -- release resource prior to error propagation
if not ok then error(err) end -- propagate
end
[1] http://lua-users.org/lists/lua-l/2010-08/msg00481.html