mustExcept

try call delegate

bool
mustExcept
(
E : Throwable = Exception
)
(
void delegate
()
fnc
,
bool throw_unexpected = true
)

Parameters

fnc void delegate
()

called delegate

throw_unexpected bool

if true when catch exception with type != E throw it out, if false ignore it

Return Value

Type: bool

true if is catched exception of type E, false otherwise

Examples

assert(  mustExcept!Exception( { throw new Exception("test"); } ) );
assert( !mustExcept!Exception( { throw new Throwable("test"); }, false ) );
assert(  mustExcept( { throw new Exception("test"); } ) );
assert( !mustExcept( { throw new Throwable("test"); }, false ) );
assert(  mustExcept!Throwable( { throw new Exception("test"); } ) );
assert(  mustExcept!Throwable( { throw new Throwable("test"); } ) );
assert( !mustExcept!Exception({ auto a = 4; }) );
static class A {}
static assert( !__traits(compiles, mustExcept!A({})) );

static class TestExceptionA : Exception
{ this() @safe pure nothrow { super( "" ); } }
static class TestExceptionB : Exception
{ this() @safe pure nothrow { super( "" ); } }
static class TestExceptionC : TestExceptionA
{ this() @safe pure nothrow { super(); } }

assert(  mustExcept!Exception({ throw new TestExceptionA; }) );
assert(  mustExcept!Exception({ throw new TestExceptionB; }) );

assert(  mustExcept!TestExceptionA({ throw new TestExceptionA; }) );
assert(  mustExcept!TestExceptionA({ throw new TestExceptionC; }) );
assert(  mustExcept!TestExceptionB({ throw new TestExceptionB; }) );

assert( !mustExcept!TestExceptionB( { throw new TestExceptionA; }, false ) );
assert( !mustExcept!TestExceptionA( { throw new TestExceptionB; }, false ) );

auto test_b_catched = false;
try mustExcept!TestExceptionA({ throw new TestExceptionB; });
catch( TestExceptionB ) test_b_catched = true;
assert( test_b_catched );

Meta