Bugzilla – Bug 497175
Exceptions thrown in a method that is invoked via Control.Invoke are not propagated to the caller
Last modified: 2009-08-13 15:37:41 UTC
Description of Problem: When Control.Invoke is used to invoke a method, if that method throws an exception it does not get propagated back to the caller - rather an unhandled exception occurs. The Microsoft implementation does propagate this exception, which can then be caught with in a try..catch block. To fix this, AsyncMethodResult.EndInvoke should throw the exception (if any was generated), and this will automatically fix Control.Invoke, and indeed any handlers in async callbacks using Control.EndInvoke. I've created a patch to introduce this functionality, but am unsure as to where to post it for review. Steps to reproduce the problem: 1. Create a method that throws an exception. 2. Call that method using Control.Invoke, and wrap it in a try..catch block Actual Results: An unhandled exception. Expected Results: A caught exception. How often does this happen? Always.
Post here the patch, and I will review it. Thanks!
Created attachment 289405 [details] A patch to introduce exception propagation to Control.Invoke
The first "throw ex;" in the patch must be a "throw;"
Besides to the comment by Robert, I think that in: @@ -513,7 +525,14 @@ try { AsyncMethodResult result = data.Result; - object ret = data.Method.DynamicInvoke (data.Args); + object ret; + try { + ret = data.Method.DynamicInvoke (data.Args); + } catch (Exception ex) { + result.CompleteWithException (ex); + return; + } + result.Complete (ret); We could move the AsyncMethodResult line out of the try block, so we can use a catch block to call result.CompleteWithException, instead of having two try blocks - which I don't like very much. Besides that, the patch is fine. Thanks!
Created attachment 311470 [details] A better patch to introduce exception propagation to Control.Invoke Okay, so I've updated the patch to introduce those changes - thanks for reviewing guys!
I applied your patch in rev 139853. Thanks! Carlos.