0

Let's say I have the following code:

private delegate void DeadlyDelegate();

private void Deadly()
{
    throw new Exception("DIE!");
}

public void DoStuff()
{
    DeadlyDelegate d = Deadly;
    d.BeginInvoke(null, null);
}

Now, there has already been much discussion about if EndInvoke() is needed (and the answer is yes), so I'm not asking that. But let's say that for whatever reason, EndInvoke is never reached (in my example, I simply don't call it, but we could imagine other reasons of how it could accidentally not ever be called).

First question: What happens to the exception? Does it completely disappear and never cause problems? Or does it finally get thrown as an unhandled exception during garbage collection, the same way that Task exceptions do if they are never observed? I've done much searching, both Google and SO, and haven't found the answer to this.

In my own code, I of course plan to always call EndInvoke(). But it would be nice to know what happens if, because of some unanticipated program path, EndInvoke somehow isn't called. I don't want my whole app to come crashing to a halt because of the unhandled exception (if there is one).

Second question: Whatever is the answer to the first question, is the same true of the built-in async calls in the .NET framework, like TcpClient.BeginConnect()? If that method throws an exception, will it simply disappear if I never call EndConnect(), or can it still cause problems?

Third question: (this should be trivial, but thought I'd double check) Is there any way some sort of strange unhandled/unobserved exception can happen in the async code between the BeginInvoke/Connect and EndInvoke/Connect calls? Or am I always guaranteed any exceptions will safely propagate to the point where EndInvoke/Connect is called?

JoeCool
  • 4,392
  • 11
  • 50
  • 66
  • 4
    Did you try and see what happens? If so, what did you learn? – Lasse V. Karlsen Jul 31 '13 at 16:10
  • Your question is answered here: http://stackoverflow.com/questions/11620310/do-you-have-to-call-endinvoke-or-define-a-callback-for-asynchronous-method-ca – Lasse V. Karlsen Jul 31 '13 at 16:12
  • Yes, I tried, even by calling GC.Collect() later to see if the exception ever shows up, but so far it hasn't. However, I saw the same behavior with Tasks, and I know that with tasks the exception *will* eventually show up in garbage collection, so the answer wasn't conclusive for me. – JoeCool Jul 31 '13 at 16:13
  • Basically, the exception is lost, it doesn't terminate your program, but it did terminate the thread that called the method. Additionally, you will have a leakage for about 10 minutes if you don't call `EndInvoke`. Basically, do the right thing. – Lasse V. Karlsen Jul 31 '13 at 16:14
  • I already read the question you linked thoroughly, but it didn't seem to answer conclusively that the exception will never show back up. It sounds like you're saying that though, so that works for me. – JoeCool Jul 31 '13 at 16:16
  • You can also see here: http://stackoverflow.com/questions/14961450/where-are-clr-defined-methods-like-delegate-begininvoke-documented – Lasse V. Karlsen Jul 31 '13 at 16:17
  • Also, neither of those links answer my second or third question, though I'd imagine people would be complaining everywhere if the answer to those questions were different than what I expect. – JoeCool Jul 31 '13 at 16:23
  • I didn't bother answering those questions, since you're supposed to write just one question at a time here on [so]. Otherwise, you're going to have multiple answers that are all correct, but only answer part of your posted question, and thus neither can be accepted alone. The answer to those questions are likely different. Especially the third, if the setup of the asynchronous method fails, then obviously it will throw right away. However, since the BeginInvoke and EndInvoke methods of delegates are not documented, exactly what kind of exceptions I don't know. Or even *if* there are any. – Lasse V. Karlsen Jul 31 '13 at 16:26
  • Thanks for the help on these questions, Lasse (for real). Though I would like to point out in general that I've noticed there's an intense sense of guilty-until-proven-innocent attitude towards questions on Stack Overflow, about if a question is legitimate or not. It's almost laughable to see how many people immediately have to claim "this is not a homework question!" and similar things to not get shot down immediately and talked to patronizingly. I get *actually nervous* when typing out a question even after searching google and SO for over two hours! That's sad to me. – JoeCool Jul 31 '13 at 17:40

0 Answers0