lua-users home
lua-l archive

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


On Jan 30, 2008 6:57 PM, Hu Qiwei <huqiwei@gmail.com> wrote:
NOTE if  'return' or 'break' exists in "cleanup block", it will not raise the error object
but just do what "return" and "break" do.

I'm not sure to understand: does it mean that the following program won't run the finally-block?

function f(filename)
   local h
   try
      h = io.open (filename)
      return h:read '*a'
   finally
      if h then h:close() end
   end
end
print(f "data.txt")

If so, it might cause pretty nasty bugs. If you can't properly handle returns and breaks in the try-block and catch-block, maybe you could check at compile time that there are no such statements, and cause an error if there are?

Will the finally-block be run if an error occurs in the catch-block?
 
The main interest of finally-blocks is that they'll be run no matter what, especially when very weird situations occur. It's rather important that this construct is extremely foolproof.

Incidentally, by writing this example, I'm thinking that maybe the catch-blocks and the finally-block should share the same scope as the try-block (i.e. if local h was declared after the "try" above, it would still be accessible to the "finally" code. Just as the "until" condition in plain Lua's "repeat...until" is in the loop body's scope). I'm interested by potential users advices, before maybe putting that in metalua 0.4.

-- Fabien.