moteus / lua-try

Simple exception support based on LuaSocket

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lua-try

Licence Build Status Coverage Status

Simple exception support based on LuaSocket

You can read this article.

Also you can check out pure Lua implementation of this library - try-lua .

Usage

local try = require "try"

local protected_fun = try.protect(function(fname)
  local f -- local file handle
  
  -- define finalizer to close file
  local assert = try.new(function()
    if f then f:close() end
  end)

  -- now if `assert` raise error then
  -- `try` calls finalizer and protected function
  -- returns nil and error object

  f = assert(io.open(fname))

  assert(do_some_function(f))

  f:close()
end)

About

Simple exception support based on LuaSocket

License:MIT License


Languages

Language:Lua 57.6%Language:C 27.2%Language:Shell 15.2%