Try blocks are trying

Paul Backus snarwin at gmail.com
Mon Oct 11 16:23:30 UTC 2021


On Monday, 11 October 2021 at 16:04:00 UTC, FeepingCreature wrote:
> Sure, but then you can't get at the `V` without - going into a 
> subscope again. :) Though I guess it wouldn't be covered by a 
> `try`.

At some point, you need to have separate code paths for the error 
case and the success case, yes. I know of no language construct 
in D that allows you to create separate code paths without 
introducing a scope somewhere. The best you can do is move the 
scopes around.

> Neat (my lang) doesn't have exceptions, but it does have 
> built-in sumtypes with error marking, so you can have
>
> ```
> (V | fail Exception) frgl() { ... }
>
> auto x <- frgl();
> ```
>
> And it will "pick" the non-fail `V` and, in the case of 
> `Exception`, just propagate (return) it.

In D, you can also write `auto x = frgl();`, and it will 
propagate the exception automatically. :)

This is not a replacement for `try` blocks, because the reason 
you use a `try` block is to handle an exception locally; in other 
words, to avoid propagating it.


More information about the Digitalmars-d mailing list