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.
Function std.exception.collectExceptionMsg
Catches the exception thrown from the given expression and returns the
msg property of that exception. If no exception is thrown, then null is
returned. E can be void.
If an exception is thrown but it has an empty message, then
is returned.
emptyExceptionMsg
Note that while can be used to collect any
collectExceptionMsgThrowable and not just Exceptions, it is generally ill-advised to
catch anything that is neither an Exception nor a type derived from
Exception. So, do not use to collect
non-collectExceptionMsgExceptions unless you're sure that that's what you really want to
do.
Prototype
string collectExceptionMsg(T, E)( E expression );
Parameters
| Name | Description |
|---|---|
| T | The type of exception to catch. |
| expression | The expression which may throw an exception. |
Example
void throwFunc() { throw new Exception("My Message."); }
assert(collectExceptionMsg(throwFunc()) == "My Message.");
void nothrowFunc() {}
assert(collectExceptionMsg(nothrowFunc()) is null);
void throwEmptyFunc() { throw new Exception(""); }
assert(collectExceptionMsg(throwEmptyFunc()) == emptyExceptionMsg);
Authors
Andrei Alexandrescu and Jonathan M Davis