return 与文件读取异常

以下代码在文件不存在的时候,会报错,但无法被try..catch..捕获。

try
{
return await File.ReadAllTextAsync(tempCopy);
}
catch (Exception ex)
{
throw new IOException($"读取 MTP 文件失败: {mtpFullPath}", ex);
}

 

改成以下代码形式,工作正常:

try
{
var result = await File.ReadAllTextAsync(tempCopy);
return result;
}
catch (Exception ex)
{
throw new IOException($"读取 MTP 文件失败: {mtpFullPath}", ex);
}

posted @ 2025-10-15 14:45  China Soft  阅读(5)  评论(0)    收藏  举报