小编Dev*_*eak的帖子

try catch块中的变量范围

我在尝试关闭Finally代码块中的文件时遇到错误:

static void Main(string[] args)
{
    try
    {
        StreamReader myReader = new StreamReader("c:\\j\\MyFile1.txt");

        string line = "";

        while (line != null)
        {
            line = myReader.ReadLine();
            Console.WriteLine(line);
        }

        //myReader.Close();
    }
    catch (FileNotFoundException e)
    {
        Console.WriteLine("sorry file not found! {0}", e.Message);
    }
    catch (DirectoryNotFoundException e)
    {
        Console.WriteLine("You have given the wrong path for the file! {0}", e.Message);
    }
    catch (Exception e)
    {
        Console.WriteLine("Sorry you typed in a wrong file name! {0}", e.Message);
    }
    finally
    { 
        myReader.Close();
    }

    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

c# scope exception-handling

0
推荐指数
2
解决办法
3700
查看次数

标签 统计

c# ×1

exception-handling ×1

scope ×1