Can anyone explain this?

Nicholas Wilson iamthewilsonator at hotmail.com
Tue Jun 5 08:26:22 UTC 2018


On Tuesday, 5 June 2018 at 08:12:54 UTC, Shachar Shemesh wrote:
> I set up to find out what happens if the assert string throws. 
> I have to admit I did not expect the result:
>
> $ cat test.d
> import std.stdio;
> import core.exception;
>
> void main() {
>     scope(failure) {
>         writeln("Not gonna happen");
>     }
>
>     try {
>         static string throwingFunc() {
>             throw new Exception("An exception");
>         }
>         assert(0==1, throwingFunc());
>     } catch(Exception ex) {
>         writeln("Exception");
>     } catch(AssertError ex) {
>         writeln("Assert");
>     }
> }
>
> $ ldc2 --version
> LDC - the LLVM D compiler (1.8.0):
>   based on DMD v2.078.3 and LLVM 5.0.1
> ...
>
> $ ./test
> Not gonna happen
> object.Exception at test.d(11): An exception

applying a bit of lowering

int Dmain (int arc, const char** argv)
{
     try return main();
     catch (...) dumpThrowable(); // 3 dumps exception
}
void main() {
     try {
         try {
              static string throwingFunc() {
                 throw new Exception("An exception");
             }
              __assert_fail(throwingFunc(),...); // 1 throws 
before assert throws
             } catch(Exception ex) {
                writeln("Exception"); // Why is this not caught? 
I've no idea
            } catch(AssertError ex) {
               writeln("Assert"); // this is not caught because 
the thrown throwable is not an exception
           }
      }
      catch(...)
      {
         writeln("Not gonna happen"); // 2 scope failure is run
           throw;
      }

}


More information about the Digitalmars-d mailing list