www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - nested delegate throwing/catching

DMD 0.125 on linux.

: import std.stdio;
:
: void main ()
: {
:
:       // if this is uncommented, whatever below delegate-throw
:       // that's first isn't caught by caughtIt()
:       void throwItCauseBug ()
:       {
:               throw new Exception("throwItCauseBug");
:       }
:
:       // this nested delegate preceeding caughtIt() doesn't cause
:       // the bug
:       void doNothing ()  { }
:
:       // uncommenting this will also cause the bug
: /+    void throwObject ()
:       {
:               throw new Object;
:       }
: +/
:
:       bool caughtIt (void delegate () dg)
:       {
:               bool caught;
:               try {
:                       dg();
:               }
:               catch (Exception e) {
:                       caught = true;
:                       writefln("caughtIt: %s", e);
:               }
:               return caught;
:       }
:
:       // nested delegate here doesn't cause the bug
:       void throwItWorks ()
:       {
:               throw new Exception("throwItWorks");
:       }
:
:       bool hmm;
:
:       // uncomment and this will be the uncaught that should be caught
: /+    hmm = caughtIt(delegate void() { throwItCauseBug(); });
:       assert (hmm);
: +/
:       // Leave throwItCauseBug uncommented and this will not be
:       // caught by caughtIt.
:       // Comment throwItCauseBug and this will be caught by caughtIt.
:       hmm = caughtIt(delegate void() {throw new Exception("dg literal");});
:       assert (hmm);
:
:       // fails if throwItCauseBug uncommented and this is the
:       // first attempt
:       hmm = caughtIt(delegate void() {throwItWorks();});
:       assert (hmm);
:
:       // also fails if throwItCauseBug uncommented and this is
:       // the first attempt
:       hmm = caughtIt(&throwItWorks);
:       assert (hmm);
:
:       // if this is first attempt, it works regardless if
:       // throwItCauseBug is uncommented
:       try {
:               throw new Object;
:       }
:       catch (Object o) {
:               writefln("main caught: %s", o);
:       }
: }
May 29 2005