[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to make sure some operations in ANY situation? (aka: with statement in Python)
- From: Peter Cawley <lua@...>
- Date: Mon, 20 Jun 2011 16:57:27 +0100
2011/6/20 Xavier Wang <weasley.wx@gmail.com>:
> local ok, rets... = pcall(func)
> pcall(cleanup())
> if not ok then
> pcall(err_func)
> error(rets...)
> end
> -- using rets, note that before here I don't actually know how many values
> the function returns
> local a,b,c = rets...
When an error occurs, there will be precisely one return value after
ok, so you can do:
local ok, a, b, c = pcall(func)
pcall(cleanup)
if not ok then
pcall(err_func)
error(a)
end
-- use a,b, c