View source code Display the source code in std/exception.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

std.exception.enforce - multiple declarations

Function enforce

If !!value is true, value is returned. Otherwise, new Exception(msg) is thrown.

Prototype

T enforce(E, T)(
  T value,
  const(char)[] msg = null,
  string file = __FILE__,
  size_t line = __LINE__
)
if (is(typeof(()
{
if (!value)
{
}
}
)));

Note

enforce is used to throw exceptions and is therefore intended to aid in error handling. It is not intended for verifying the logic of your program. That is what assert is for. Also, do not use enforce inside of contracts (i.e. inside of in and out blocks and invariants), because they will be compiled out when compiling with -release. Use assert in contracts.

Example

auto f = enforce(fopen("data.txt"));
auto line = readln(f);
enforce(line.length, "Expected a non-empty line.");

Function enforce

Deprecated. If passing the file or line number explicitly, please use the overload of enforce which takes them as function arguments. Taking them as template arguments causes unnecessary template bloat. This overload will be removed in June 2015.

Prototype

T enforce(T, string file, ulong line)(
  T value,
  const(char)[] msg = null
)
if (is(typeof(()
{
if (!value)
{
}
}
)));

Function enforce

If !!value is true, value is returned. Otherwise, the given delegate is called.

The whole safety and purity are inferred from Dg's safety and purity.

Prototype

T enforce(T, Dg, string file, ulong line)(
  T value,
  Dg dg
)
if (isSomeFunction!Dg && is(typeof(dg())) && is(typeof(()
{
if (!value)
{
}
}
)));

Function enforce

If !!value is true, value is returned. Otherwise, ex is thrown.

Prototype

T enforce(T)(
  T value,
  Throwable ex
);

Example

auto f = enforce(fopen("data.txt"));
auto line = readln(f);
enforce(line.length, new IOException); // expect a non-empty line

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0

Comments