Specification of executing order of multiple scope(s)
Steven Schveighoffer
schveiguy at gmail.com
Thu Jun 1 15:34:21 UTC 2023
On 5/31/23 11:52 AM, An Pham wrote:
> Should the scope failure to be executed before exit regardless of the
> code order?
>
> import std.exception, std.stdio;
>
> void test()
> {
> writeln("test");
>
> scope (failure)
> {
> writeln("failure");
> }
>
> scope (exit)
> {
> writeln("exit");
> }
>
> throw new Exception("exception");
> }
>
> void main()
> {
> try
> {
> test();
> }
> catch (Exception e) writeln(e.msg);
> }
>
> Output
> test
> exit
> failure
> exception
From the [D
spec](https://dlang.org/spec/statement.html#scope-guard-statement):
```
If there are multiple ScopeGuardStatements in a scope, they will be
executed in the reverse lexical order in which they appear.
```
-Steve
More information about the Digitalmars-d
mailing list