function try(func)
local ok, err = pcall(func)
return {
catch = function(self, handle)
if not ok then
handle(err)
end
return self
end,
finally = function(self, handle)
handle()
return self -- Optional
end
}
end
try(function()
error "Error!"
end)
:catch(function(err)
print(err)
end)
:finally(function()
print "Finally!"
end)
-- Same as:
local ok, err = pcall(function() error "Error!" end)
if not ok then
print(err)
end
print "Finally!"
I think the chances of Lua having try-catch-finally statements would
be zero, pcall is good enough.