Created
September 9, 2011 23:06
-
-
Save cwarden/1207556 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "try-catch" | |
try { | |
function() | |
error('oops') | |
end, | |
catch { | |
function(error) | |
print('caught error: ' .. error) | |
end | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Why not just use pcall()
with error()
inside in case of wrongs?
appears to be a much cleaner way than employ tables with functions to emulate this. I know, sometimes we feel comfortable with our language skills but know a language way to do things may be more efficient and resources friendly.
how would it look like? @arkt8
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.
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
Sensational idea !!
This is my suggestion to include "finally" in a different style approach: