www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - scope(failure) compiles but is not called if it inside of block

reply Denis Feklushkin <feklushkin.denis gmail.com> writes:
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
next sibling parent reply Denis Feklushkin <feklushkin.denis gmail.com> writes:
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?
Jan 31 2019
parent Denis Feklushkin <feklushkin.denis gmail.com> writes:
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
Jan 31 2019
prev sibling next sibling parent Neia Neutuladh <neia ikeran.org> writes:
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
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Jan 31 2019