Skip to content

Instantly share code, notes, and snippets.

@cwarden
Created September 9, 2011 23:06
Show Gist options
  • Save cwarden/1207556 to your computer and use it in GitHub Desktop.
Save cwarden/1207556 to your computer and use it in GitHub Desktop.
require "try-catch"
try {
function()
error('oops')
end,
catch {
function(error)
print('caught error: ' .. error)
end
}
}
function catch(what)
return what[1]
end
function try(what)
status, result = pcall(what[1])
if not status then
what[2](result)
end
return result
end
@arkt8
Copy link

arkt8 commented Nov 17, 2022

ok, res = pcall(fn, arg1 ... argn)
if not ok then
  -- treat here the catch --
end
-- normal flow --

I had not benchmarked with your code, but using table members always lead to performance degrading when comparing with local variables. Also it is the main purpose of pcall.

@Nico-Pergande
Copy link

ok, res = pcall(fn, arg1 ... argn)
if not ok then
  -- treat here the catch --
end
-- normal flow --

I had not benchmarked with your code, but using table members always lead to performance degrading when comparing with local variables. Also it is the main purpose of pcall.

so I would print the res with error() in the catch if clause?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment