Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

stein197/lua-catchify

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tiny try-catch-finally implementation for Lua

Lua does not have a common syntax sugar to wrap the code that will possibly lead to errors. This package provides a very simple way to bring this sugar to Lua.

Installation

Via LuaRocks:

luarocks install catchify

Or just download and require init.lua file from this repo.

Usage

The whole concept can be illustrated in a single code piece below:

local try = require "catchify"
try(function ()
	error "Something's wrong!"
end):catch(function (e) -- e will contain error message
	print(e) -- > stdin:3: Something's wrong!
end):finally(function () -- Will be executed anyway
	print "Finally here!"
end)

You can pass a table containing single function instead of a function to make syntax more "curly":

try {
	function ()
		error "Something's wrong!"
	end
} :catch {
	function (e)
		print(e)
	end
} :finally {
	function ()
		print "Finally here!"
	end
}

Testing

Install luaunit package:

luarocks install luaunit

Then run from the console:

lua test.lua

About

Tiny try-catch-finally statement for Lua

Resources

License

Stars

3 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.