lua-users home
lua-l archive

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


On Thu, Sep 11, 2014 at 09:50:29AM -0400, Sean Conner wrote:
> It was thus said that the Great Andrew Starks once stated:
<snip>
> > Are there interesting differences between how you think about / deal with
> > error handling in C/C++ vs. Lua?  Accepting that the mechanisms are
> > different, do you do more "try to recover" code in C?
> 
>   I don't program in C++, so I don't use exceptions.  In regard to error
> handling, I tend to handle errors in Lua like I do in C.  And I don't really
> do "recover" code in C.  [2][3]
> 

What do you mean by recover? Surely you handle errors like EMFILE and ENOMEM
without exiting the process.

Handling errors is much easier if one follows the RAII pattern.

RAII is scripture in the C++ community, but ultimately all it means is that
you arrange for a strict hierarchy of objects, with clear and simple object
ownership roles, and acquire scarce resources as soon as possible and in
tandem, typically in constructors. All those things together mean errors
tend to group into and be handled by relatively isolated portions of your
code where you can easily implement and test recovery logic, regardless of
whether the language supports exceptions or some kind of GC (partial GC in
the case of C++).

In C it helps if you use failure-free data structures as much as possible,
which is why I never use list or tree implementations that do dynamic node
allocation. Otherwise, you need to maintain additional state and write
additional logic to handle partial rewinding of application state.