Zure Zure - 1 month ago 13
C# Question

FileNotFoundException, but catch block is not entered (Async method)

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:

enter image description here

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?

Answer

In the Exception Popup i see [x] Break when this exception type is thrown. Try disabling it there, or from the Exception Settings dialog (Ctrl-Alt-E on my system, or menu Debug > Windows > Exception Settings).

Comments