问问题描述
c#task中的异常怎么捕获,求大佬施舍一个解决方案,感激不尽!
答精选答案

大致有两种方式
1) 调用 Task 的 Wait 方法时使用 try-catch 捕获异常:
2)在 Task 的 ContinueWith 方法中读取 Task 的 Exception 属性:
var testTask = TestAsync(5, -10);testTask.ContinueWith(task => { if (task.IsFaulted){Console.WriteLine(task.Exception.GetBaseException()); } else{Console.WriteLine(task.Result); }});
本文来自作者[IT风尚]投稿,不代表公众科技网立场,如若转载,请注明出处:https://www.cpst.net.cn/miaozhi/1962996.html
评论列表(4条)
我是公众科技网的签约作者“IT风尚”!
希望本篇文章《c#task中的异常怎么捕获》能对你有所帮助!
本站[公众科技网]内容主要涵盖:教育咨询,知识百科
本文概览:大致有两种方式1) 调用 Task 的 Wait 方法时使用 try-catch 捕获异常:2)在 Task 的 ContinueWith 方法中读取 Task 的 Exception 属性:var testTask = TestAsync(5, -10);testTask.ContinueWith(task => { if (task.IsFaulted){Console.WriteLine(task.Exception.GetBaseException()); } else{Console.Write