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

IGotD- nise at nise.com
Sat Feb 26 20:59:53 UTC 2022


On Friday, 25 February 2022 at 21:03:21 UTC, forkit wrote:
>
> The 'real' motivation is simple -> an increased awareness of 
> the benefits of Rust within the C++ community.

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.

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). D can even handle multiple exceptions, even if that is 
seldom used. Basically you can catch any exception up the call 
chain and polymorphism really makes this possible. It is easy to 
add new exceptions as well as reusing existing ones thanks to 
this.

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. Rust might also 
provide an Error trait (Rust way of structs with virtual methods) 
which is more generic so that you can add your own custom error 
type. However, this Error trait is in general allocated on the 
heap so not even Rust is immune to heap allocated error handling.

For simpler error handling Result<T, E> is a better match as E is 
in general an immutable string, an integer or nothing.





More information about the Digitalmars-d mailing list