What should happen when the assert message expression throws?

Meta jared771 at gmail.com
Fri Nov 18 13:04:30 UTC 2022


On Friday, 18 November 2022 at 12:36:14 UTC, RazvanN wrote:
> Unfortunately, I don't see a clear way to rewrite this to valid 
> D code as to catch the exception and then assert. Ideally, we 
> could do something along the lines of:
>
> ```d
> assert(i == 42,
>        (const(char)[] msg,
>         try { msg = format("Bad parameter:", i); },
>         catch (Exception e) { msg = "Assert message evaluation 
> has failed";},
>         msg)
>        );
> ```

Why not rewrite it like so?

```d
assert(i == 42, ()
{
     try
     {
         return format("Bad parameter:", i);
     }
     catch (Exception e)
     {
         return "Assert message evaluation has failed";
     }
}());
```


More information about the Digitalmars-d mailing list