Skip to content
stevedonovan edited this page Feb 18, 2013 · 7 revisions

Although people might think that a modern language needs try/catch, the usual Lua pcall idiom is not as clumsy in MoonScript, especially if we specialize it for the case where the function return value is not important.

For example,

try = (f) ->
    ok,err = pcall f
    if not ok then err\gsub '^[^:]+:%d+: ',''

err = try ->
    print a.x
if err
    print 'error!',err

--~ error!  attempt to index global 'a' (a nil value)

Clone this wiki locally