async/await וניהול שגיאות
הנה קוד לדוגמא:
myMethod אורך זמן ממושך, ואני עוטף אותו בtask.
הבעיה היא במקרה שמתחוללת שגיאה, הcatch איננו מופעל. אודה מאד למי שיעזור לי.
string s;
try { s = await myMethodeAsync(); } catch { s = "error"; } } async Task<string> myMethodeAsync() { string result = await Task.Run(() => myMethod()); return result; } string myMethod() { throw new Exception("some error"); }
הנה קוד לדוגמא:
myMethod אורך זמן ממושך, ואני עוטף אותו בtask.
הבעיה היא במקרה שמתחוללת שגיאה, הcatch איננו מופעל. אודה מאד למי שיעזור לי.
private async void Button_Click(object sender, RoutedEventArgs e) {string s;
try { s = await myMethodeAsync(); } catch { s = "error"; } } async Task<string> myMethodeAsync() { string result = await Task.Run(() => myMethod()); return result; } string myMethod() { throw new Exception("some error"); }