On 05/29/2013 06:25 AM, Jakob Ovrum wrote:

> I was unable to leverage std.exception.

I have looked only your short example. std.exception.ifThrown may work:

import std.typecons;
import std.exception;

alias ToThrow = Flag!"ToThrow";

// Can throw, and we want to catch
int createTheVar(ToThrow toThrow)
{
    if (toThrow == ToThrow.yes) {
        throw new Exception("bad cat!");
    }

    return 42;
}

// Can also throw, but we don't want to catch it here
int transform(int a)
{
    return a + 1;
}

int foo(ToThrow toThrow)
{
    const(int) i = createTheVar(toThrow).ifThrown!Exception(666);
    return transform(i);
}

unittest
{
    assert(foo(ToThrow.yes) == 667);
    assert(foo(ToThrow.no) == 43);
}

void main()
{}

Ali

Reply via email to