lua-users home
lua-l archive

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


All the examples given involves invoking a function in in pcall. Wouldn't be nice if LUA by his own create a function and invokate that function with pcall automatically when we do something like this:

try 
   -- protected code. A function/closure would be automatically created with this code. Will be called with "pcall"
catch (e)
   -- catch code. Another function/closure would be automatically created with this code.
finally
   -- finally code. One more function/closure woluld be automatically create with this code.
end;

We would have clean code for exception handling using the already given suggestions managed by LUA.
I am "in love" for LUA and I miss "native" exception handling statements. I think this way would be a relatively easy way to give to the language this ability natively.


On Tue, May 31, 2016 at 6:15 AM, Peter Melnichenko <mpeterval@gmail.com> wrote:
My somewhat unfinished version: https://github.com/mpeterv/goodcall

Pros:
* Supports Python's `else` block, which is executed only if `try` block
  didn't throw.
* Allows rethrowing without losing original traceback.
* Supports yielding from protected blocks a-la coxpcall, but unlike it
  doesn't lose tracebacks in any case.

Cons:
* Rethrown errors are tables {err, traceback}.
  If some errors are part of an API, users have to know how to check
  if an error is wrapped and how to unwrap it. Or they have to use my
module, too.
* Rethrown errors have __tostring metamethod to make them look nice
  when caught by something that just prints errors. Unfortunately, Lua
interpreters
  for different versions handle such table errors differently, with
Lua 5.1 replacing
  it with `(error object is not a string)`.

-- Peter




--
Alysson Cunha / AlyssonRPG
http://www.rrpg.com.br - Jogue o tradicional RPG de mesa online
http://www.alyssoncunha.com.br - Me conheça melhor