Jump to page: 1 2
Thread overview
Tricky code with exceptions
May 09, 2013
bearophile
May 09, 2013
Mike James
May 09, 2013
Maxim Fomin
May 09, 2013
bearophile
May 09, 2013
Sean Kelly
May 09, 2013
Brad Anderson
May 09, 2013
Brad Anderson
May 09, 2013
bearophile
May 10, 2013
evilrat
May 10, 2013
Juan Manuel Cabo
May 10, 2013
Juan Manuel Cabo
May 10, 2013
Dan Olson
May 09, 2013
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
"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
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
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
For what it's worth, this runs fine on 64-bit OSX.
May 09, 2013
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
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
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 10, 2013
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 10, 2013
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
« First   ‹ Prev
1 2