[Issue 16960] implicit function return breaks chaining of exceptions

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Dec 14 15:56:27 PST 2016


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

Ali Cehreli <acehreli at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|implicit function return    |implicit function return
                   |breaks chaining of          |breaks chaining of
                   |exceptions thrown in        |exceptions
                   |scope(exit)                 |

--- Comment #3 from Ali Cehreli <acehreli at yahoo.com> ---
The issue remains even if I don't throw from within a scope(exit) block.
Uncomment any 'return' inside foo() and the code works as expected:

import std.stdio;
import std.string;

struct ThrowsInDestructor {
    int n;

    ~this() {
        throw new Exception(format("thrown for %s", n));
    }
}

void foo(int n) {
    writeln("foo called with ", n);
    auto throws = ThrowsInDestructor(n);

    if (n > 0) {
        /* return */ foo(n - 1);
        // return;
    }
    // return;
}

void main() {
    // Never mind the unconventional range limits:
    // Throws one exception for each value in the range 0..n, including n.
    enum chainLength = 3;
    enum expectedLength = chainLength + 1;

    try {
        foo(chainLength);
    }
    catch (Exception original) {
        size_t count = 0;
        for (Throwable ex = original; ex; ex = ex.next) {
            writeln(ex.msg);
            ++count;
        }
        if (count != expectedLength) {
            writefln("Expected %s but walked %s links", expectedLength, count);
            writefln("\nTHE ORIGINAL EXCEPTION:\n\n%s", original);
        }
    }
}

--


More information about the Digitalmars-d-bugs mailing list