Alternatives to exceptions for error handling
H. S. Teoh
hsteoh at quickfur.ath.cx
Tue Nov 24 22:14:03 UTC 2020
On Tue, Nov 24, 2020 at 01:30:33PM -0700, Jonathan M Davis via Digitalmars-d wrote:
> On Monday, November 23, 2020 3:58:29 AM MST Jacob Carlborg via Digitalmars-d
> > [...] I think without any form of language support and syntax sugar,
> > error handling is going to be a pain.
> [...] in general, exceptions are by far the cleanest and simplest way
> to deal with error conditions, and I would _really_ hate to see D move
> away from them as the default error handling mechanism.
I've said before, and I'll say again: I suspect that most (all?) of the
current objections against exceptions comes from the current
*implementation* of it, i.e., libunwind, et al.. There are other ways
to implement the concept of exceptions that can potentially avoid these
For example, instead of inserting stack frames and stack unwinding code,
just use a dedicated register or CPU flag that indicates an error in the
caller. The callee detects this and branches to the cleanup code at the
end of the function. Then try/throw/catch become essentially just
syntactic sugar for C-style error return codes. The syntactic function
return value essentially behaves like an implicit out parameter that
conveys the return value. Most complex C code is written more-or-less
along these lines anyway, so this isn't really anything new.
Interfacing with C/C++ code will be trickier, of course, but not
insurmountable. E.g., make it so that extern(C) will conform to the
usual ABI, but extern(D) will use the new function call convention. Then
native D code can benefit from this, while still be able to interface
with C code via an extern(C) layer.
> Personally, I really don't care much about how they're implemented
> underneath the hood, but the ability to throw an error condition up to
> code that handles it higher up, allowing all of the code in between to
> largely ignore the error condition is huge, and a lot of code can get
> pretty disgusting without that.
Береги платье снову, а здоровье смолоду.
More information about the Digitalmars-d