collectException and nothrow
Jonathan M Davis via Digitalmars-d
digitalmars-d at puremagic.com
Sat Nov 8 21:44:36 PST 2014
On Sunday, November 09, 2014 03:15:21 Casey via Digitalmars-d wrote:
> Good evening,
>
> I have a question for you. Why does collectException violate the
> noThrow attribute? I would have thought that since it captures
> an Exception, it would allow the method that normally would throw
> an exception to be used within a noThrow method. I did get the
> same effect by nesting it within a assumeNoThrow method, but I'm
> still curious as to why collectException doesn't make the same
> guarantee.
>
> Btw: this is with dmd 2.066.1.
>
> Thanks.
Are you looking at the documentation or attempting to compile code?
collectException isn't going to be marked as nothrow, because it can only be
nothrow if it's collectException!Exception (which _is_ the default), because
it can only be nothrow if no Exceptions (be they Exception or derived from
Exception) can escape the function - e.g. collectException!MyException can't
be nothrow - and neither can stuff like collectException!Error or
collectException!AssertError. collectException _should_ be inferred as
nothrow so long as it's collectException!Exception (and
collectException!Throwable should work as well, though I don't know if it
currently does).
If I compile this code on my machine using 2.066.1:
import std.exception;
import std.stdio;
void func()
{
throw new Exception("hello");
}
void main() nothrow
{
auto e = collectException(func());
}
it compiles just fine. So, it seems like nothrow is being properly inferred
for collectException, and I don't know what you're doing that isn't working.
Please provide a code snippet that you think should work but doesn't.
- Jonathan M Davis
P.S. In the future, please post questions like this is D.learn. The main
newsgroup/mailing list/forum is for discussing the language itself, not for
asking questions about how to use it or how it works (or doesn't work).
- Jonathan M Davis
More information about the Digitalmars-d
mailing list