Asserts inside nothrow function
Denis Koroskin
2korden at gmail.com
Sat May 23 07:12:29 PDT 2009
I just started using nothrow functions a little bit and came across the following issue.
Some of the functions that I'd like to mark as nothrow have debug asserts inside them.
They used to throw an Exception, but I'd like to change their behavior and terminate an application whenever an assertion fails inside a nothrow function.
It's not very handy to write
try { assert(condition, errorMessage); } catch { abort(); }
So, how about criticalEnforce inside std.contracts? Something like this:
import std.stdio;
extern (C) void abort() nothrow;
auto tryOrDie(T)(scope lazy T dg) nothrow
{
try {
return dg();
} catch (Exception e) {
try {
writeln("Exception occurred: ", e);
} catch {
}
abort();
}
}
auto criticalEnforce(T...)(T args) nothrow
{
return tryOrDie(enforce(args));
}
BTW, an attempts to use it leads to an ICE:
Assertion failure: '0' on line 939 in file 'glue.c'
More information about the Digitalmars-d
mailing list