Thread overview
collectException and nothrow
Nov 09, 2014
Casey
Nov 09, 2014
Daniel Murphy
Nov 09, 2014
Jonathan M Davis
Nov 10, 2014
Casey
November 09, 2014
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.
November 09, 2014
"Casey"  wrote in message news:kymxuhfmugteozqlfgmo@forum.dlang.org...

> 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.

import std.exception;

void main() nothrow
{
   collectException({ throw new Exception(null); });
}

Works fine to me.  Are you catching 'Exception' or catching a specific Exception subtype?  If the latter, there's your answer - other types of exceptions will still leak through. 

November 09, 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

November 10, 2014
> 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

First, sorry about that.  I must have glossed over the d.learn forum.

Second, I actually did compile and run it and got the failure.  However, reading your response, I'm not wondering if I did make a mistake.  In fact, I just tried it again and it did work.  Regardless, what you said makes sense.  Thanks.