这是我第一次使用async await,我正在使用它来加载大型XML文件。但是,我无法捕获XML异常错误。谁能给我指个方向?
try
{
XElement element = await Task.Run(() => XElement.Load(filePath));
}
catch (System.Xml.XmlException ex)
{
MessageBox.Show(ex.Message);
}发布于 2020-02-22 23:58:45
我能够用下面的代码捕获异常。也可以在Try/Catch Wrap Around Task.Run not Handling Exception上找到。
try
{
Exception exceptionOut = null;
await Task.Run(() =>
{
try
{
XElement.Load(filePath);
}
catch (Exception exceptionIn)
{
exceptionOut = exceptionIn;
}
});
if (exceptionOut != null)
{
throw exceptionOut;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}https://stackoverflow.com/questions/60352316
复制相似问题