Created
December 22, 2024 19:27
-
-
Save Zbizu/3cfe6fd468c1f79e9cb930bbfa8a78f1 to your computer and use it in GitHub Desktop.
Lua port of "try catch finally"
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
-- Lua try / catch port | |
-- example: | |
--[[ | |
TryCatch( | |
-- try | |
function() | |
-- code to execute | |
-- throw a message | |
throw("some message") | |
end, | |
-- catch | |
function(exceptionMessage) | |
print(exceptionMessage) | |
end, | |
-- finally | |
function() | |
-- do something | |
end | |
) | |
]] | |
function TryCatch(try, catch, finally) | |
local status, message = pcall(try) | |
if catch and not status then | |
catch(message) | |
end | |
if finally then | |
finally() | |
end | |
end | |
throw = error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment