www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Tricky code with exceptions

reply "bearophile" <bearophileHUGS lycos.com> writes:
A little Java program I've just found in a blog post:


class Flow {
     static public void main(String[] args) {
         for (int i = 0; i < 6; ++i) {
             System.out.println("Loop: " + i);

             try {
                 try {
                     if (i == 3)
                         break;
                 } finally {
                     if (i % 2 != 0)
                         throw new Exception("");
                 }
             } catch (Exception e) {
                 System.out.println("Caught");
             }
         }
     }
}


Its output:

Loop: 0
Loop: 1
Caught
Loop: 2
Loop: 3
Caught
Loop: 4
Loop: 5
Caught


My D translation:

import std.stdio;

void main() {
     foreach (i; 0 .. 6) {
         writeln("Loop: ", i);

         try {
             try {
                 if (i == 3)
                     break;
             } finally {
                 if (i % 2 != 0)
                     throw new Exception("");
             }
         } catch (Exception e) {
             writeln("Caught");
         }
     }
}


It prints:

Loop: 0
Loop: 1
Caught
Loop: 2
Loop: 3

And then it crashes.

Bye,
bearophile
May 09 2013
next sibling parent "Mike James" <foo bar.com> writes:
"bearophile" <bearophileHUGS lycos.com> wrote in message 
news:pnwldlckpgrjvvujepzo forum.dlang.org...

<SNIP>

 My D translation:

 import std.stdio;

 void main() {
     foreach (i; 0 .. 6) {
         writeln("Loop: ", i);

         try {
             try {
                 if (i == 3)
                     break;
             } finally {
                 if (i % 2 != 0)
                     throw new Exception("");
             }
         } catch (Exception e) {
             writeln("Caught");
         }
     }
 }


 It prints:

 Loop: 0
 Loop: 1
 Caught
 Loop: 2
 Loop: 3

 And then it crashes.

 Bye,
 bearophile
Strangely, if you replace the "break" instruction with "continue" (I know it's pointless code), it also crashes... Regards, Mike.
May 09 2013
prev sibling next sibling parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
It works on dpaste http://dpaste.dzfl.pl/fcd2f2b5 which seems to 
be based on linux 2.62. Which platform do you use?

P.S. Seems we can define new kind of forum contribution - some 
code works as expected in language X, but equivalent code in D 
goes ballistic. This is very sad.
May 09 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Maxim Fomin:

 It works on dpaste http://dpaste.dzfl.pl/fcd2f2b5 which seems 
 to be based on linux 2.62.
On dpaste it also works on gdc2.060 and ldc2.060, both 64 bit.
 Which platform do you use?
Vista 32. Probably I will add it to Bugzilla.
 P.S. Seems we can define new kind of forum contribution - some 
 code works as expected in language X, but equivalent code in D 
 goes ballistic. This is very sad.
On the other hand often it's broken code in other languages that works as desired in D :-) Bye, bearophile
May 09 2013
prev sibling next sibling parent "Sean Kelly" <sean invisibleduck.org> writes:
For what it's worth, this runs fine on 64-bit OSX.
May 09 2013
prev sibling parent reply "Brad Anderson" <eco gnuk.net> writes:
On Thursday, 9 May 2013 at 11:24:03 UTC, bearophile wrote:
 A little Java program I've just found in a blog post:


 class Flow {
     static public void main(String[] args) {
         for (int i = 0; i < 6; ++i) {
             System.out.println("Loop: " + i);

             try {
                 try {
                     if (i == 3)
                         break;
                 } finally {
                     if (i % 2 != 0)
                         throw new Exception("");
                 }
             } catch (Exception e) {
                 System.out.println("Caught");
             }
         }
     }
 }


 Its output:

 Loop: 0
 Loop: 1
 Caught
 Loop: 2
 Loop: 3
 Caught
 Loop: 4
 Loop: 5
 Caught


 My D translation:

 import std.stdio;

 void main() {
     foreach (i; 0 .. 6) {
         writeln("Loop: ", i);

         try {
             try {
                 if (i == 3)
                     break;
             } finally {
                 if (i % 2 != 0)
                     throw new Exception("");
             }
         } catch (Exception e) {
             writeln("Caught");
         }
     }
 }


 It prints:

 Loop: 0
 Loop: 1
 Caught
 Loop: 2
 Loop: 3

 And then it crashes.

 Bye,
 bearophile
I just tested this for you when you hopped in IRC but you left before I could tell you that a 64-bit Windows dmd build did not crash and here is the output. Loop: 0 Loop: 1 Caught Loop: 2 Loop: 3 Caught Loop: 4 Loop: 5 Caught
May 09 2013
next sibling parent "Brad Anderson" <eco gnuk.net> writes:
On Thursday, 9 May 2013 at 18:24:46 UTC, Brad Anderson wrote:
 I just tested this for you when you hopped in IRC but you left 
 before I could tell you that a 64-bit Windows dmd build did not 
 crash and here is the output.
Oh, and this was dmd 2.062 (just -m64).
May 09 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Brad Anderson:

 a 64-bit Windows dmd build did not crash and here is the output.
Thank you for the data point. So maybe the problem is only on 32 bit Windows. Bye, bearophile
May 09 2013
parent reply "evilrat" <evilrat666 gmail.com> writes:
On Thursday, 9 May 2013 at 20:33:17 UTC, bearophile wrote:
 Brad Anderson:

 a 64-bit Windows dmd build did not crash and here is the 
 output.
Thank you for the data point. So maybe the problem is only on 32 bit Windows. Bye, bearophile
win8 dmd 2.062 32-bit also crashes
May 09 2013
next sibling parent reply "Juan Manuel Cabo" <juanmanuel.cabo gmail.com> writes:
Tested on Linux - Kubuntu 12.04 64bits
       dmd 2.058 -m32
       dmd 2.058 64 bits
       dmd 2.062 -m32
       dmd 2.062
and the output is this for all of the above:

     Loop: 0
     Loop: 1
     Caught
     Loop: 2
     Loop: 3
     Caught
     Loop: 4
     Loop: 5
     Caught

Then, tested on Windows XP SP3 32 bits, dmd 2.062,
and it DIDN'T CRASH. The output is:

     Loop: 0
     Loop: 1
     Caught
     Loop: 2
     Loop: 3

--jm
May 09 2013
parent "Juan Manuel Cabo" <juanmanuel.cabo gmail.com> writes:
On Friday, 10 May 2013 at 01:05:39 UTC, Juan Manuel Cabo wrote:
 Then, tested on Windows XP SP3 32 bits, dmd 2.062,
 and it DIDN'T CRASH. The output is:
Mm, sorry!!, it did crash but I had dr watson disabled in that VM. Confirmed adding a writeln("finished") after the foreach, which isn't reached. So It DID CRASH in Windows XP 32 bits. Sorry for the noise. --jm
May 09 2013
prev sibling parent Dan Olson <zans.is.for.cans yahoo.com> writes:
"evilrat" <evilrat666 gmail.com> writes:

 On Thursday, 9 May 2013 at 20:33:17 UTC, bearophile wrote:
 Brad Anderson:

 a 64-bit Windows dmd build did not crash and here is the output.
Thank you for the data point. So maybe the problem is only on 32 bit Windows. Bye, bearophile
win8 dmd 2.062 32-bit also crashes
Anybody looked at the assembly for the crashing executables? The truth is in there.
May 09 2013