[OT] - C++ exceptions are becoming more and more problematic

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Feb 26 23:56:31 UTC 2022


On Sat, Feb 26, 2022 at 08:59:53PM +0000, IGotD- via Digitalmars-d wrote:
[...]
> It seems to be some kind false narrative that Rust is savior of all
> and will create a new world. The more I learn Rust, the more I realize
> that's not the case.

Exactly.  The Rust hype these days are reminiscient of the Java hype
back in the 90's.  Java was touted as the be-all and end-all of
programming that will solve all of your programming problems and present
none of its own. Well, 30 years later we have learned some good things
from Java, but we have also realized its shortcomings, and that it's
really NOT what it was originally hyped to be.


> If you look at Rust error handling (the proposed std::Result) it is
> less powerful than exceptions in D (or C++, C# and several others).
[...]
> If you look at Rust Result<T, E>, it is basically a wrapped return
> value and the type in E must be known to the caller so cannot easily
> pass it up the chain. Type E can be anything but adding your own error
> types becomes cumbersome.
[...]

Thanks, that's exactly what I've been trying to say, except you said it
better. :-D

Error handling using sum types, etc., are at the core just a cosmetic
makeover of C's return codes.  It puts the onus on the caller to check
for every return code and take appropriate action. AND it's not easily
propagated up the call chain, because the error type E returned by func1
may not be the same type E returned by its caller, so you cannot simply
pass it along; you *have* to manually map it to your own error return
type.

In theory, your own error return type would be a sum type of all error
returns of the functions you call plus your own error codes. In
practice, I honestly doubt if anyone would bother doing this; I expect
the tendency would be to simply map every error from dependent functions
to the equivalent of INTERNAL_ERROR, i.e., a generic, non-descript,
unhelpful code that tells the caller nothing about what the actual error
is.

Basically, it would be identical to how you use error codes in C, except
dressed in prettier syntax.  The fundamental problems with C error code
handling remain unchanged.

For example, it does not address the issue that often, the information
required to decide how to handle an error is not readily available in
the caller, but can only be found higher up the call chain. The Rust way
is just glorified C return codes that doesn't solve this problem in any
way.


T

-- 
People demand freedom of speech to make up for the freedom of thought which they avoid. -- Soren Aabye Kierkegaard (1813-1855)


More information about the Digitalmars-d mailing list