Last frame?

bearophile bearophileHUGS at lycos.com
Sun Jun 26 05:53:28 PDT 2011


I have recently closed this bug report about File error messages:
http://d.puremagic.com/issues/show_bug.cgi?id=4911

Because now this program gives a better error message:


import std.stdio: File;
void foo() {
    auto f = File("test.raw", "r");
    f.write("hello");
}
void bar() {
    foo();
}
void main() {
    bar();
}



With DMD 2.053 it gives at runtime (using -g):

std.exception.ErrnoException at std\stdio.d(286): Cannot open file `test.raw' in
mode `r' (No such file or directory)
----------------
...\test.d(8): void test.bar()
...\test.d(10): _Dmain
----------------



The error is generated by this part of the File struct:

    this(string name, in char[] stdioOpenmode = "rb")
    {
        p = new Impl(errnoEnforce(.fopen(name, stdioOpenmode),
                        text("Cannot open file `", name, "' in mode `",
                                stdioOpenmode, "'")),
                1, name);
    }



But the stack trace stops at the function before the one that has generated the error. I don't know enough about the implementation of the exceptions, but is it possible to invent a Phobos function that returns the name of the penultimate function? To be used like this:


    this(string name, in char[] stdioOpenmode = "rb")
    {
        p = new Impl(errnoEnforce(.fopen(name, stdioOpenmode),
                        text("File usage in function `", lastFunctionName(),
                             "', cannot open file `", name, "' in mode `",
                             stdioOpenmode, "'")),
                1, name);
    }


That prints something like:

std.exception.ErrnoException at std\stdio.d(286): File usage in function `test.foo', cannot open file `test.raw' in mode `r' (No such file or directory)

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list