/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/standard/parallel-programming/snippets/cs/exception1.cs
39 строк
1 KB
David Pine
Address concerns about chaining tasks and lacking continuation examples with .Unwrap() (#19611)
28 июл 2020, 15:00
Не верифицирован
28 июл 2020, 15:00
9685f88
Код
Авторство
О чём код?
using System; using System.Threading.Tasks; public class ExceptionExample { public static async Task Main() { Task<int> task = Task.Run( () => { Console.WriteLine($"Executing task {Task.CurrentId}"); return 54; }); var continuation = task.ContinueWith( antecedent => { Console.WriteLine($"Executing continuation task {Task.CurrentId}"); Console.WriteLine($"Value from antecedent: {antecedent.Result}"); throw new InvalidOperationException(); }); try { await task; await continuation; } catch (Exception ex) { Console.WriteLine(ex.Message); } } } // The example displays the similar output: // Executing task 1 // Executing continuation task 2 // Value from antecedent: 54 // Operation is not valid due to the current state of the object.