lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Sometime on 17 Dec 2007 Patrick Donnelly observed:
>I believe this error message is incorrect:
>
>> error(coroutine.create(function() end))
>2

and a little later:
>In a related matter, pcall is supposed to return the error message,
>but in fact returns whatever is passed to error()'s first argument.
>Whether this is intended I am not sure.

This is correct behavior, and is even documented as the Lua
way to do more general exception handling. It is discussed
in Programming in Lua.

See section 8.4 (http://www.lua.org/pil/8.4.html):
>Despite its name, the error message does not have to be a string.
>Any Lua value that you pass to error will be returned by pcall:
>
>    local status, err = pcall(function () error({code=121}) end)
>    print(err.code)  -->  121
>
>These mechanisms provide all we need to do exception handling in

This does make it tempting to say

	local try, throw = pcall, error

however, despite the missing analog to catch.  I suppose a token
filter could be written to turn try/catch into an if based on
pcall and an anonymous function. The downside of that would be
the extra closure creation each time through the passage.