Skip to content

Instantly share code, notes, and snippets.

@jacksonie
Forked from Zbizu/trycatch.lua
Created June 15, 2025 20:20
Show Gist options
  • Save jacksonie/339860df08816422d4f6a17ae4cb823d to your computer and use it in GitHub Desktop.
Save jacksonie/339860df08816422d4f6a17ae4cb823d to your computer and use it in GitHub Desktop.

Revisions

  1. @Zbizu Zbizu created this gist Dec 22, 2024.
    36 changes: 36 additions & 0 deletions trycatch.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    -- 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