lua-users home
lua-l archive

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


That "fake" piece of code is so ugly, which makes difficult to others programmers understand your code. 

Besides, it does not handle "finally" correctly because, in case of "finally  execution due error, the error must be raised again

Em Sáb, 15 de set de 2018 10:40, Sam Putman <atmanistan@gmail.com> escreveu:


On Sat, Sep 15, 2018 at 3:00 PM Peter Hickman <peterhickman386@googlemail.com> wrote:
On Sat, 15 Sep 2018 at 11:58, Alysson Cunha <alyssonrpg@gmail.com> wrote:
try-catch and try-finally language sugars for pcall to simplify error handling

This is the one thing that would make things much easier for me. I know we should wrap things in pcall incase they fail but there are so many things that *could* fail the resulting code becomes unreadable very quickly

A few judicious try-catch blocks can provide much robustness without obscuring the code. I tend to write code in a more iterative fashion with a few large scoped try-catch blocks (or begin-rescue-end as I'm actually a Ruby programmer) until the part of the code that is fragile comes to light


This can be adequately faked:

-- try
local success, err = pcall(function()
    -- your chunk goes here
end)
-- catch
if not success then
   -- do stuff with err
end
-- finally



 
I understand that this may be more a case of "trying to write Ruby in Lua" but I miss this more I miss OOP. I have no problem with Lua not being an OOP language, it's easy to adapt to. But catching errors seems to require chopping up the code in unnatural ways so that it can be stuffed into a function that can be pcalled

Otherwise it's still a wonderful language