Marcel Overweel wrote:
> Hi,
>
> I've got a problem with exception handling when using async methods.
> Actually, the code seems to runs fine, but the debugger reports that an
> exception is not handled by user code, while in fact it is handled properly.
>
> Basicly, I start a method using a delegate's BeginInvoke().
> In this method, an exception is raised.
> In the callback methode, I catch this exception and store it in a variable
> of a custom asyncresult object.
>
> [...]
> So why do I get that message from the debugger?
Because that's exactly what happened. When the exception was actually
thrown, the first exception handler that is executed is not in your user
code. It's in the asynchronous delegate execution code in .NET. After
all, how would your callback get called unless some .NET code knows that
your delegate method is done executing, unless some other code caught
that exception?
You don't get to see the exception until you call EndInvoke(), which you
didn't do until _after_ your callback was called, so you necessarily
have no way to see the exception until after .NET's already had a chance
to handle it.
When the exception is first thrown, .NET catches it, saves the exception
information so it can be presented again later during the EndInvoke()
method, and then calls your callback method. When your code actually
gets to catch it, the exception is being thrown for the second time.
> If it's just an annoying message, I can live with it.
I don't know why it's annoying for the debugger to truthfully tell you
exactly what happened, but yes...if you find it annoying, you'll have to
live with it.
Pete