I have a method in which I'm checking if a file exists in my filesystem (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;
}
}
When I'm trying to get a non-existing file with StorageFile.GetFileFromPathAsync
, I get an exception. For some reason it's not handled by my catch block, but looks like this instead:
What I've tried:
- Explicitly handle a
FileNotFoundException
- Add a Try-catch-block inside of the if-statement
Note that the method needs to stay asynchronous because I have other stuff going on in this method which I removed in order to provide a Minimal, Complete, and Verifiable example.
Why is the debugger not entering my catch block when a FileNotFoundException
is thrown?