digitalmars.D - scope(failure) compiles but is not called if it inside of block
- Denis Feklushkin (22/22) Jan 31 2019 code: https://run.dlang.io/is/GgQjfV
- Denis Feklushkin (3/4) Jan 31 2019 Forgot to add a question: it is valid behaviour or some bug?
- Denis Feklushkin (4/8) Jan 31 2019 Ah, scope applies its effect on the block, not on the function.
- Neia Neutuladh (4/7) Jan 31 2019 This is its own scope. Aside from the `scope(failure)` bit, it contains ...
- Jonathan M Davis (54/76) Jan 31 2019 Note that the keyword is _scope_. scope(failure) will run if an exceptio...
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
Jan 31 2019
On Thursday, 31 January 2019 at 20:05:31 UTC, Denis Feklushkin wrote:code: https://run.dlang.io/is/GgQjfVForgot to add a question: it is valid behaviour or some bug?
Jan 31 2019
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:Ah, scope applies its effect on the block, not on the function. /Threadcode: https://run.dlang.io/is/GgQjfVForgot to add a question: it is valid behaviour or some bug?
Jan 31 2019
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.
Jan 31 2019
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 failedNote 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
Jan 31 2019