Comparing Exceptions and Errors

Ali Çehreli acehreli at yahoo.com
Tue Jun 7 22:54:54 UTC 2022


On 6/7/22 12:58, frame wrote:

 > ```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.

WAT! I think that's a bug. Simply having a 'return' statement suppresses 
rethrowing the exception? Too subtle! The following bug is related:

   https://issues.dlang.org/show_bug.cgi?id=21443

And the spec does not mention 'return' in scope(failure) having such a 
huge effect. Uncommenting the 'return' line below causes wildly 
different program behavior:

import std.stdio;

void main() {
   writeln("main is calling zar");
   zar();
}

void zar() {
   scope (failure) {
     writeln("zar is failing");
   }

   writeln("zar is calling bar");
   bar();
}

void bar() {
   scope (failure) {
     writeln("bar is failing");
     // return;
   }

   writeln("bar is calling foo");
   foo();
}

void foo() {
   writeln("foo is throwing");
   throw new Exception("Oops!");
}

BUG! :)

Ali



More information about the Digitalmars-d-learn mailing list