Simple exception support based on LuaSocket
You can read this article.
Also you can check out pure Lua implementation of this library - try-lua .
local try = require "try"
local protected_fun = try.protect(function(fname)
local f -- local file handle
-- define finalizer to close file
local assert = try.new(function()
if f then f:close() end
end)
-- now if `assert` raise error then
-- `try` calls finalizer and protected function
-- returns nil and error object
f = assert(io.open(fname))
assert(do_some_function(f))
f:close()
end)