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

IGotD- nise at nise.com
Thu Feb 24 20:34:03 UTC 2022


On Thursday, 24 February 2022 at 19:14:39 UTC, Walter Bright 
wrote:
>
> I agree, which makes this a challenge.
>
> One solution is to design the return value so that failure is a 
> valid part of the type. For example, searching for X can return 
> an empty result, rather than a special "not found" error.
>
> Another solution is to redesign the problem. For example, 
> currently Phobos throws on an invalid Unicode character. A 
> better way is to treat such as a "replacement character".

Exceptions are nice because they go under the hood and we can 
catch them ... if we want to. Problem of replacing with return 
values are for example indexing an array and the ArrayIndexError 
exception. 90% of the code we don't check for this as it would be 
a bug in our code but it's a good exception that enables us to 
quickly find the bug. How would it be possible to replace this 
with a return value? One that comes to mind is a tagged union. In 
this case we would want this to be handled silently and behave 
like an exception in readable code. .unwrap hell á la Rust is 
something I want to avoid. However, there are several other ways.

Exceptions from a readable code point of view is desirable, it's 
just that it might be bad for code generation and performance. 
Would should look into fixing this first before we start to 
design something completely new. If we design something 
completely new, then we should make it as similar as traditional 
exceptions.


More information about the Digitalmars-d mailing list