Jack
Jack

Reputation: 950

Throwing FileNotFoundException but not catching

I am getting a FileNotFoundException while trying to execute a function with a try-catch block. I've tried catching a FileNotFoundException, to no avail. Can anyone tell me why it does this?

public static bool IsKeyValid(string path)
{
    bool rVal = false;

    try
    {
        Stream stream = File.Open(path + "\\data.bin", FileMode.Open);
        BinaryFormatter bf = new BinaryFormatter();

        ValidKey vk = (ValidKey)bf.Deserialize(stream);
        if (vk.SerialNumber != null)
            rVal = true;
        else
            rVal = false;

    }
    catch (Exception fnfe)
    {
            rVal = false;
    }
    return rVal;
}

Upvotes: 0

Views: 1357

Answers (2)

Bevan
Bevan

Reputation: 44327

The catch you have will catch all exceptions, but depending on how you have Visual Studio configured, it might still stop on the line raising the exception to give you a chance at debugging before the handler kicks in.

Go to the Debug|Exceptions menu to control this.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503290

My guess is that it's breaking into the FileNotFoundException in the debugger when it's initially thrown, but that it would be correctly caught by the catch block. You can change the debugger settings for exceptions - or just run it outside the debugger, of course.

Upvotes: 2

Related Questions