Lua already has the necessary mechanisms to do something completely different from exceptions. Namely pcall .
You can use pcall to execute any Lua function. If this function (or any function it calls) calls error ( assert calls error if the assertion condition is incorrect) then flow control will return to the pcall statement pcall . pcall will return false and an error message (which is passed error ).
With this, you can throw the bugs and catch them. Your "attempt" is just pcall ; your "catch" is what checks pcall result.
Also, remember: Lua is garbage collection. You do not need to perform any cleaning operations. Or, if you do, you need to modify any Lua module. Lua APIs must be Lua APIs, not C or C ++ APIs.
source share