Task
will catch exceptions for you. If you call task.Wait()
, it will wrap all detected exceptions in an AggregateException
and throw it.
[HandleError] public void FooAsync() { AsyncManager.OutstandingOperations.Increment(); AsyncManager.Parameters["task"] = Task.Factory.StartNew(() => { try { DoSomething(); } // no "catch" block. "Task" takes care of this for us. finally { AsyncManager.OutstandingOperations.Decrement(); } }); } public ActionResult FooCompleted(Task task) { // Exception will be re-thrown here... task.Wait(); return View(); }
Just adding the [HandleError]
attribute is not enough. Since the exception occurs in another thread, we must return the exception to the ASP.NET thread in order to do something with it. Only after we eliminate the exception from the right place, the [HandleError]
attribute will be able to do its job.
Daniel Schilling
source share