dereferencing null

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Mar 5 05:30:14 PST 2012


Personally I'd love to get more info about out-of-bounds errors. E.g.
for arrays, which index the code attempted to access, and for hashes
which key.

Sure it's easy to use an enforce, but I'd rather have that inserted in
debug builds with bounds checking anyway. For example:

void main()
{
    int[int] aa;
    int key = 1;
    auto a = aa[key];
}

core.exception.RangeError at test(22): Range violation

That's not too much information (well there's also that stacktrace
which is still broken on XP regardless of dbghelp.dll). This is
better:

import std.exception;
import core.exception;

void main()
{
    int[int] aa;
    int key = 1;
    enforce(key in aa, new RangeError(format(": Key %s not in hash. ", key)));
    auto a = aa[key];
}

core.exception.RangeError@: Key 1 not in hash. (20): Range violation

I'd rather not have to depend on debuggers or code duplication (even
mixins) for this basic information.

Side-note: RangeError is missing a constructor that takes a *message*
as the first parameter, the one that was called takes a file string
parameter. With the ctor fixed the error becomes:
core.exception.RangeError at test.d(20): Range violation: Key 1 not in hash.

That would help me so much without having to change code, recompile,
and then wait 20 seconds at runtime to reach that failing test again.


More information about the Digitalmars-d mailing list