Program logic bugs vs input/environmental errors
Walter Bright via Digitalmars-d
digitalmars-d at puremagic.com
Tue Oct 14 20:17:33 PDT 2014
On 10/14/2014 8:03 PM, Dicebot wrote:
> On Saturday, 11 October 2014 at 22:06:38 UTC, Walter Bright wrote:
>> All assert actually does is call a function in druntime. You can override and
>> insert your own assert handling function, and have it do as you need. It was
>> deliberately designed that way.
>
> A while ago I have been a looking for a way to throw Exception instead of Error
> for failing assertions inside unittest blocks but druntime didn't seem to
> provide needed context. Do you think this can be a worthy addition?
assert() in a unittest calls:
__d_unittest(string file, uint line)
which calls:
onUnittestErrorMsg(string file, size_t line, string msg)
which calls:
extern (C) void onAssertError( string file = __FILE__, size_t line =
__LINE__ ) nothrow
{
if( _assertHandler is null )
throw new AssertError( file, line );
_assertHandler( file, line, null);
}
You can use setAssertHandler() to set _assertHandler to do whatever you wish.
However, the compiler is still going to regard the assert() as nothrow, so the
unwinding from an Exception won't happen until up stack a throwing function is
encountered.
More information about the Digitalmars-d
mailing list