在Try / Catch中内置消息而不是我自己的消息

时间:2012-07-27 06:10:58

标签: c#

当我使用Try / Catch打开文件时,当我尝试打开不存在的文件时,程序显示内置消息而不是Catch部分中的消息。什么是错的,我错过了什么?

 public void ReadFromFile(MainFrame obj, string filePath)
    {
        try
        {
            filestream = new FileStream(filePath, FileMode.Open);
            BinaryFormatter b = new BinaryFormatter();
            var animals2 = (List<Animal>)b.Deserialize(filestream);

            foreach (Animal animal in animals2)
            {
                AddAnimalToList(animal);
                obj.UppdateListOfRegistredAnimals(animal.ID, animal.Name, animal.Age, animal.Gender);

            }
            obj.UpdateId(animals.Count());
        }
        catch
        {
            MessageBox.Show("Test", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        finally
        {
            filestream.Close();
        }
    }

编辑:我发现内置消息的原因是在上面的代码之前的某个地方!在处理openFileDialog中的事件的下面的代码中必定是错误的,因为尽管有几个消息框,但是显示了非!我做错了什么!?帮助是精确的!

private void menuFileOpen_Click(object sender, EventArgs e)
    {
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        string file = openFileDialog1.FileName;

        if (result == DialogResult.OK)
        {
            MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            try
            {
                MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                motelManager.ReadFromFile(this, file); // Smart lösning!!

            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Error message", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }

File not found message

2 个答案:

答案 0 :(得分:1)

可能是因为您试图关闭filestream块中的finally而不检查它是否为非空。如果

filestream = new FileStream(filePath, FileMode.Open);

失败,

finally
{
    filestream.Close();
}
文件流为空时执行

尝试将其更改为

finally
{
    if (filestream != null) filestream.Close();
}

或者更好的是,将filestream包裹在using块中。

using (filestream = new FileStream(filePath, FileMode.Open))
{
    // Do stuff with your filestream
} // filestream.Dispose() automatically called, which in turn calls .Close()

答案 1 :(得分:1)

试试这个

if(!File.Exists(filePath))
   MessageBox.Show("Not found", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

了解更多信息 http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

private void menuFileOpen_Click(object sender, EventArgs e)
    {
        openFileDialog1.CheckFileExists = false;
        openFileDialog1.CheckPathExists = false;

        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.


        if (result == DialogResult.OK)
        {
            string file = openFileDialog1.FileName;


            MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            try
            {
                MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                motelManager.ReadFromFile(this, file); // Smart lösning!!

            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Error message", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }

如果您不希望找不到文件的默认消息,请使用

        openFileDialog1.CheckFileExists = false;
        openFileDialog1.CheckPathExists = false;