lua-users home
lua-l archive

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

On Mon, 20 Jun 2011 11:25 -0300, "Roberto Ierusalimschy" <> wrote:
> So, to make sure that some operations MUST happen just put them out of
> the 'if':
>   if pcall(foo) then
>     <error handling code>
>   end
>   <MUST happen code>

This is neat, simple, and subtly wrong (which is perhaps why other
languages have unwind-protect et al. in the first place).

  function risky_operation() ... end
  function handle_error(err)
    local processed = gyrate_and_maybe_oom(err)

  local ok, x = pcall(risky_operation)
  if not ok then handle_error(x) end
  -- Last call doesn't always happen!

So instead the translation looks more like:

  local ok, x = pcall(risky_operation)
  -- Eats some double faults, but often okay.  Can also do
  -- plain cleanup_everything() to have cleanup errors squash
  -- operation errors and be propagated further, or other
  -- fancier tricks...
  if not ok then handle_error(x); return end

   ---> Drake Wilson