On Wednesday, 24 December 2014 at 13:48:26 UTC, ketmar via Digitalmars-d-learn wrote:
the `[email protected]` is not a backtrace result, this is the
result of Exception class constructor:

this (string msg, string file=__FILE__, usize line=__LINE__, Throwable next=null)

it registering the file and line of exception object creation in
COMPILE time. so you can do nothing with it, as there is no way to know
what called what in compile time.

p.s. if you can use GDC, for example, and turn on debug info,
backtrace will show you files and line numbers for every address.

Hmm, that makes sense. After some pondering, I think I've hacked together a workaround though:

    template check(alias func) {
auto check(string file = __FILE__, int line = __LINE__)(int x) {
            auto result = func(x);
            if (result < 0) // throw on negative return vaule
throw new Exception("%d < 0".format(result), file, line);
            return x; // otherwise, pass the result through
        }
    }

which produces:

    2
    [email protected](23): -3 < 0
    ----------------
    .. (_Dmain+0x20) [0x448de4]


Are there any hidden downsides to doing it this way (aside from generating additional code on every call)?

Reply via email to