How to catch RangeError in unittest?

wjoe invalid at example.com
Tue Feb 18 13:07:35 UTC 2020


I have a function add(index, data) which throws RangeError if the 
index is invalid.

unittest { // is supposed to test that RangeError is thrown on a 
range violation
    assertThrown!RangeError(add(size_t.max, something)); //which 
doesn't work, the test is killed with core.exception.RangeError: 
Range violation

    // this kills the test, too: core.exception.RangeError: Range 
violation
    try {
      add(size_t.max, something);
      assert(false, "Expecting RangeError");
    } catch (RangeError) {}
}

Just for clarification, I do _not_ intend to catch RangeError 
outside of a unittest. What I want is that the test acknowledges 
the correctly thrown RangeError and gracefully reports success.


What are my options?


More information about the Digitalmars-d-learn mailing list