抛出FileNotFoundException但没有捕获 [英] Throwing FileNotFoundException but not catching

查看:137
本文介绍了抛出FileNotFoundException但没有捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用try-catch块执行功能时遇到FileNotFoundException异常.我试图捕获FileNotFoundException,但无济于事.谁能告诉我为什么这样做吗?

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;
}

推荐答案

我的猜测是,当它最初被抛出时,它正在进入调试器中的FileNotFoundException,但是它会被catch块正确捕获.您可以更改调试器的异常设置-当然,也可以在调试器之外运行它.

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.

这篇关于抛出FileNotFoundException但没有捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆