KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Assume the following synchronous code: try { Foo(); Bar(); Fubar(); Console.WriteLine("All done"); } catch(Exception e) // For illustration purposes only. Catch specific exceptions! { Console.WriteLine(e); } Now assume all these methods have an Async counterpart and I have to use those for some reason, so simply wrapping the whole thing in a new task is not an option. How would I achieve the same behavior? What I mean with "same" is: Execute a handler for the exception, if one is thrown. Stop execution of the following methods, if an exception is thrown. The only thing I was able to come up with is horrible : var fooTask = FooAsync(); fooTask.ContinueWith(t => HandleError(t.Exception), TaskContinuationOptions.OnlyOnFaulted); fooTask.ContinueWith( t => { var barTask = BarAsync(); barTask.ContinueWith(t => HandleError(t.Exception), TaskContinuationOptions.OnlyOnFaulted); barTask.ContinueWith( t => { var fubarTask = FubarAsync(); fubarTask.ContinueWith(t => HandleError(t.Exception), TaskContinuationOptions.OnlyOnFaulted); fubarTask.ContinueWith( t => Console.WriteLine("All done"), TaskContinuationOptions.OnlyOnRanToCompletion); }, TaskContinuationOptions.OnlyOnRanToCompletion); }, TaskContinuationOptions.OnlyOnRanToCompletion); Please note: I need a solution that works with .NET 4, so async/await is out of the question. However, if it would work with async/await feel free to show how. I don't need to use the TPL. If it is impossible with the TPL another approach would be OK, maybe w
Tags (comma-separated)
Save Edits
Cancel