The solution to "Error handling"...

Ali Çehreli acehreli at yahoo.com
Sat Jul 4 03:52:58 UTC 2026


On 7/3/26 4:08 PM, Dennis wrote:

 > "Positive
 > Conversion Overflow". Without attaching more information to Exceptions
 > in intermediate catch blocks, the error is devoid of context.

Yes but I automated that for a C++ library.

 > (Unless
 > you print the stack trace but it's not like a regular user can make any
 > sense of that)

I used a set of macros to print a stack of error messages.

 > current trendy thing is returning `Result<Value, Error>` types

I don't know what Error is for so I returned e.g. ReturnValue<int>.

 > like Rust does.

No, I did it my way. :D

 > I don't have that much Rust experience, but I have seen code
 > full of `result.expect("quick message")`

The ugliness that came with macros was

ReturnValue<int> foo(int i) {
     TRY(bar(i) == 42
         MSG("Failed to do 'bar' for %d", i));

     return 100;
}

And bar(i) contains other TRY, etc. macros to stack error messages for 
context.

What D intentionally lacks from C++:

1) Macros like TRY can inject statements like 'return failure;' D cannot 
(does not) do that.

2) TRY internally returns 'failure' upon failure, a value of a special 
type that implicitly converts to ReturnValue<T>. Similarly, 'return 100' 
above is an implicit conversion. D cannot (does not) do that.

It worked very well for me.

Ali



More information about the Digitalmars-d mailing list