0

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?

Community
  • 1
  • 1
Dennis Schröer
  • 2,392
  • 16
  • 46

2 Answers2

3

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).

Peter B
  • 22,460
  • 5
  • 32
  • 69
0
 var item = await StorageFile.GetFileFromPathAsync(filename); 

Did you have try and catch in this function ?? if yes, add

throw 

in catch

try this :Why can't I catch an exception from async code? and this : Catch an exception thrown by an async method can help you.

Community
  • 1
  • 1