View source code
Display the source code in std/exception.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.exception.assertThrown

Asserts that the given expression throws the given type of Throwable. The Throwable is caught and does not escape assertThrown. However, any other Throwables will escape, and if no Throwable of the given type is thrown, then an AssertError is thrown.

void assertThrown(T, E) (
  lazy E expression,
  string msg = null,
  string file = __FILE__,
  size_t line = __LINE__
);

Parameters

NameDescription
T The Throwable to test for.
expression The expression to test.
msg Optional message to output on test failure.
file The file where the error occurred. Defaults to __FILE__.
line The line where the error occurred. Defaults to __LINE__.

Throws

AssertError if the given Throwable is not thrown.

Example

import core.exception : AssertError;
import std.string;

assertThrown!StringException(enforce!StringException(false, "Error!"));

//Exception is the default.
assertThrown(enforce!StringException(false, "Error!"));

assert(collectExceptionMsg!AssertError(assertThrown!StringException(
           enforce!StringException(true, "Error!"))) ==
       `assertThrown failed: No StringException was thrown.`);

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0