On 2/1/13, Andrej Mitrovic <[email protected]> wrote:
> Self pro-tip: Wrap it in a template

Here, and I didn't even have to use string mixins:

import std.format;

template Wrap(alias func, Exceptions...)
{
    auto ref Wrap(string file = __FILE__, size_t line = __LINE__,
Args...)(Args args)
    {
        foreach (Ex; Exceptions)
        {
            try
            {
                return func(args);
            }
            catch (Ex exc)
            {
                exc.file = file;
                exc.line = line;
                throw exc;
            }
        }

        assert(0);  // silence compiler
    }
}

void foo(int x)
{
    if (x)
        throw new Exception("");
    else
        throw new FormatException("");
}

void main()
{
    foo(0);  // L37: [email protected](32):
    foo(1);  // L38: [email protected](30):

    alias Wrap!(foo, FormatException, Exception) myFoo;
    myFoo(0);  // L41: [email protected](41):
    myFoo(1);  // L42: [email protected](42):
}

Reply via email to