Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 31, 2019 scope(failure) compiles but is not called if it inside of block | ||||
---|---|---|---|---|
| ||||
code: https://run.dlang.io/is/GgQjfV /++ dub.sdl: name "ttest" description "test" +/ import std.stdio; import std.exception; void main() { scope(failure) writeln("1"); { scope(failure) writeln("2"); // compiles but is not called for some reason } scope(failure) writeln("3"); enforce(false); // throws exception } Outpupt: Running ./ttest 3 1 object.Exception@bitop_bt.d(19): Enforcement failed |
January 31, 2019 Re: scope(failure) compiles but is not called if it inside of block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Feklushkin | On Thursday, 31 January 2019 at 20:05:31 UTC, Denis Feklushkin wrote:
> code: https://run.dlang.io/is/GgQjfV
Forgot to add a question: it is valid behaviour or some bug?
|
January 31, 2019 Re: scope(failure) compiles but is not called if it inside of block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Feklushkin | On Thursday, 31 January 2019 at 20:07:32 UTC, Denis Feklushkin wrote:
> On Thursday, 31 January 2019 at 20:05:31 UTC, Denis Feklushkin wrote:
>> code: https://run.dlang.io/is/GgQjfV
>
> Forgot to add a question: it is valid behaviour or some bug?
Ah, scope applies its effect on the block, not on the function.
/Thread
|
January 31, 2019 Re: scope(failure) compiles but is not called if it inside of block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Feklushkin | On Thu, 31 Jan 2019 20:05:31 +0000, Denis Feklushkin wrote:
> {
> scope(failure) writeln("2");
> }
This is its own scope. Aside from the `scope(failure)` bit, it contains no code. No code means no exceptions are thrown. Therefore the `writeln("2")` bit never gets called.
|
January 31, 2019 Re: scope(failure) compiles but is not called if it inside of block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Feklushkin | On Thursday, January 31, 2019 1:05:31 PM MST Denis Feklushkin via Digitalmars-d wrote:
> code: https://run.dlang.io/is/GgQjfV
>
> /++ dub.sdl:
> name "ttest"
> description "test"
> +/
>
> import std.stdio;
> import std.exception;
>
> void main()
> {
> scope(failure) writeln("1");
>
> {
> scope(failure) writeln("2"); // compiles but is not
> called for some reason
> }
>
> scope(failure) writeln("3");
>
> enforce(false); // throws exception
> }
>
>
> Outpupt:
> Running ./ttest
> 3
> 1
> object.Exception@bitop_bt.d(19): Enforcement failed
Note that the keyword is _scope_. scope(failure) will run if an exception is thrown after the scope statement within the current scope. Braces add a new scope.
{
scope(failure) writeln("2");
}
is essentially equivalent to
{
try
{
}
catch(Exception e)
{
writeln("2");
throw e;
}
}
There is no code within the try block which could possibly throw, so the catch block will never run. Your entire function is essentially the same as
void main()
{
try
{
{
try
{
}
catch(Exception e)
{
writeln("2"); // compiles but is not called for some reason
throw e;
}
}
try
{
enforce(false); // throws exception
}
catch(Exception e)
{
writeln("3");
throw e;
}
}
catch(Exception e)
{
writeln("1");
throw e;
}
}
BTW, questions about D really belong in D.Learn. The main newsgroup is intended for general discussions about D, not for asking questions about it.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation