Here are the two alternatives i tried for catching errors, they both seem to do the same thing.. but is one preferable over the other and why ?
Alt 1:
private async void BtnClickEvent(object sender, RoutedEventArgs e) { try { Task t = Task.Run(() => { _someObj.SomeMethod(); }); await t; //wait here, without blocking... } catch (Exception ex) { string errMsg = ex.Message + Environment.NewLine; errMsg += "some unhandled error occurred in SomeMethod"; Log(errMsg); return; //<-- bypass below code on error... } //other code below... does not execute... DoSomethingElse(); } Alt 2:
private async void BtnClickEvent(object sender, RoutedEventArgs e) { bool errOccurred = false; Task t = Task.Run(() => { try { _someObj.SomeMethod(); } catch (Exception ex) { string errMsg = ex.Message + Environment.NewLine; errMsg += "some unhandled error occurred in SomeMethod"; Log(errMsg); errOccurred = true; }//end-Catch }); await t; //wait here, without blocking... if (errOccurred) return; //<-- bypass below code on error... //other code below... does not execute... DoSomethingElse(); }
Task.Run) and could test it without any threading issues