【发布时间】:2016-10-21 15:16:24
【问题描述】:
我有一种方法可以检查文件系统 (UWP) 中是否存在文件:
private async Task<bool> FileExistsAsync(string filename)
{
try
{
if (filename != null)
{
var item = await StorageFile.GetFileFromPathAsync(filename); // FileNotFoundException happens here
if (item != null)
return true;
}
return false;
}
catch (Exception e) // I Expect the debugger to handle the FileNotFoundException here, but it never happens
{
return false;
}
}
当我尝试使用StorageFile.GetFileFromPathAsync 获取不存在的文件时,我遇到了异常。由于某种原因,我的 catch 块没有处理它,而是看起来像这样:
我的尝试:
- 显式处理
FileNotFoundException - 在 if 语句中添加 Try-catch-block
请注意,该方法需要保持异步,因为我在此方法中还有其他内容,我删除了这些内容以提供Minimal, Complete, and Verifiable example。
为什么在抛出 FileNotFoundException 时调试器没有进入我的 catch 块?
【问题讨论】:
-
试试这个。捕捉(异常){返回假; }
-
@vivek 它没有改变任何东西。另外,我需要代码中的参数(正如我所说,我删除了一些不必要的部分)
标签: c# exception asynchronous exception-handling uwp