簡體   English   中英

如何捕獲“FileNotFoundException”異常?

[英]How to catch a "FileNotFoundException" exception?

我對編程很陌生,正在嘗試找出如何捕獲錯誤“FileNotFoundException”。 我的代碼假設搜索現有的文本文檔(從輸入到文本框中的內容)並將其加載到我的 listbox1。 我解決了這個問題。 然而,新的問題出現了! 如果用戶輸入錯誤的名稱/數字,它只會使應用程序崩潰,並顯示找不到文件的錯誤。 有什么辦法可以讓程序顯示錯誤消息“找不到文件”。 或者干脆不讓整個程序崩潰? 提前致謝!

     private void btnEnter_Click(object sender, EventArgs e)
    {   
        FileInfo file = new FileInfo(txtExisting.Text + ".txt");
        StreamReader stRead = file.OpenText();
        while (!stRead.EndOfStream)
        {
            listBox1.Items.Add(stRead.ReadLine()); 
        }  
    }

您應該使用 try-catch 語句來處理異常。

private void btnEnter_Click(object sender, EventArgs args)
{   
    try
    {
        FileInfo file = new FileInfo(txtExisting.Text + ".txt");
        StreamReader stRead = file.OpenText();
        while (!stRead.EndOfStream)
        {
            listBox1.Items.Add(stRead.ReadLine()); 
        }  
    } 
    catch (FileNotFoundException e)
    {
        // FileNotFoundExceptions are handled here.
    }
}

基本上, try塊中的代碼將像往常一樣執行,但如果出現錯誤,則將執行catch塊,特別是:

引發異常時,公共語言運行時 (CLR) 會查找處理此異常的 catch 語句。

這意味着如果您希望遇到不同類型的異常,try-catch 語句可以有多個catch塊,以便可以相應地處理它們。

可以在此處找到更多信息。

至於用戶體驗,最好通過顯示消息向用戶傳達出現問題的信息。

只需在btnEnter_Click函數中使用您的代碼添加一個 try/catch 塊,如下所示:

try
{
   //your code here
}
catch (FileNotFoundException ex)
{
    MessageBox.Show(ex.Message);//if you want to show the exception message
}
catch (Exception ex1)
{
    /* Exceptions other than the above will be handled in this section,
     this should be used when you are not  aware the type of exception 
     can occur in your code for a safe side*/
}

使用try/catch語句:

private void btnEnter_Click(object sender, EventArgs e)
{   
    try
    {
        FileInfo file = new FileInfo(txtExisting.Text + ".txt");
        StreamReader stRead = file.OpenText();
        while (!stRead.EndOfStream)
        {
            listBox1.Items.Add(stRead.ReadLine()); 
        }  
    }
    catch (FileNotFoundException ex)
    {
        // Handle exception
    }
}

使用System.IO.File.Exist("path\\\\File.extension");

File.Exists / MSDN它將返回一個布爾值,為找到文件為true ,為未找到文件為false 當您不知道什么可能導致問題時,請使用 try/catch 語句。

例子:

private void btnEnter_Click(object sender, EventArgs e)
    {   
        if(!System.IO.File.Exists(txtExisting.Text + ".txt")
        {
              MessageBox.Show("File not found");
              return;
        }
        FileInfo file = new FileInfo(txtExisting.Text + ".txt");
        StreamReader stRead = file.OpenText();
        while (!stRead.EndOfStream)
        {
            listBox1.Items.Add(stRead.ReadLine()); 
        }  
    }
        FileInfo file = new FileInfo(txtExisting.Text + ".txt");
        if (!File.Exists(file.FullName))
        {
            Console.WriteLine("File Not Found!");
        }
        else
        {
            StreamReader stRead = file.OpenText();
            while (!stRead.EndOfStream)
            {
                lenter code hereistBox1.Items.Add(stRead.ReadLine());
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM