Comparing Exceptions and Errors
frame
frame86 at live.com
Tue Jun 7 19:58:03 UTC 2022
On Tuesday, 7 June 2022 at 18:37:13 UTC, Steven Schveighoffer
wrote:
>
> My very common use of `scope(failure)` for my DB code:
>
> ```d
> conn.exec("START TRANSACTION");
> scope(success) conn.exec("COMMIT");
> scope(failure) conn.exec("ROLLBACK");
> ```
>
> This is hard to encapsulate into a type, as dtors only hook
> `scope(exit)` essentially.
>
> -Steve
That's fine as the Throwable is still thrown but since
`scope(failure)` acts as like catch-block, people may use it as
replacement if there is something more to do.
I personally like the clean, short syntax when I don't care about
the actual exception or if I know the called function has already
handled the exception and just rethrows it:
```d
bool fun() {
scope(failure) {
// do something
return false;
}
badCode();
return true;
}
```
I know this is bad as I'm capturing a possible error here - but
this is what an unexperienced coder would do, because it works.
The better approach would be to use `scope(exception)` (not
typed, just as keyword) that allows to act only on Exceptions so
one cannot break Exception vs Error paradigma and this also
clarifies that usage of `scope(failure)` is potentially dangerous
if you return from it.
Anyway, I just put that here - maybe you mention it in your blog.
More information about the Digitalmars-d-learn
mailing list