问题描述
privateasyncvoidbutton1_Click(objectsender,EventArgse){try{awaitDoSomethingAsync();}catch(Exceptionex){MessageBox.Show(ex.Message);}}privateasyncTaskDoSomethingAsync(){awaitTask.Run(()=>{Thread.Sleep(3000);thrownewException("Test");});}
以上代码写法并不能catch到DoSomethingAsync抛出的异常,求问正确写法,谢谢~
解决方案
解决方案二:
Task<int>t=null;if(t.Exception!=null){throwt.Exception;}
时间: 2025-01-21 10:01:17