Throw

Force all integral errors to fail by throwing an exception of type Throw.CheckFailure. The message coming with the error is similar to the one printed by Warn.

Members

Classes

CheckFailure
class CheckFailure

Exception type thrown upon any failure.

Static functions

hookOpCmp
int hookOpCmp(Lhs lhs, Rhs rhs)

Called automatically upon a comparison for ordering using one of the operators <, <=, >, or >=. In case the comparison is erroneous (i.e. it would make a signed negative value appear greater than or equal to an unsigned positive value), throws a Throw.CheckFailure exception. Otherwise, the three-state result is returned (positive if lhs > rhs, negative if lhs < rhs, 0 otherwise).

hookOpEquals
bool hookOpEquals(L lhs, R rhs)

Called automatically upon a comparison for equality. Throws upon an erroneous comparison (one that would make a signed negative value appear equal to an unsigned positive value).

onBadCast
Dst onBadCast(Src src)

Called automatically upon a bad cast (one that loses precision or attempts to convert a negative value to an unsigned type). The source type is Src and the destination type is Dst.

onLowerBound
T onLowerBound(Rhs rhs, T bound)

Called automatically upon a bounds error.

onOverflow
typeof(~Lhs()) onOverflow(Lhs lhs)
typeof(Lhs() + Rhs()) onOverflow(Lhs lhs, Rhs rhs)

Called automatically upon an overflow during a unary or binary operation.

onUpperBound
T onUpperBound(Rhs rhs, T bound)

Called automatically upon a bounds error.

Examples

void test(T)()
{
    Checked!(int, Throw) x;
    x = 42;
    auto x1 = cast(T) x;
    assert(x1 == 42);
    x = T.max + 1;
    import std.exception : assertThrown, assertNotThrown;
    assertThrown(cast(T) x);
    x = x.max;
    assertThrown(x += 42);
    assertThrown(x += 42L);
    x = x.min;
    assertThrown(-x);
    assertThrown(x -= 42);
    assertThrown(x -= 42L);
    x = -1;
    assertNotThrown(x == -1);
    assertThrown(x == uint(-1));
    assertNotThrown(x <= -1);
    assertThrown(x <= uint(-1));
}
test!short;
test!(const short);
test!(immutable short);

Meta