try catch块中的变量范围

时间:2013-07-31 17:12:50

标签: c# exception-handling scope

我在尝试关闭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();
}

5 个答案:

答案 0 :(得分:5)

您需要在StreamReader之上声明try

话虽这么说,我建议在这种情况下使用using语句而不是try / finally,因为它专门用于资源清理。

using (StreamReader myReader = new StreamReader("c:\\j\\MyFile1.txt"))
{
    try
    {
        string line = "";

        while (line != null)
        {
            line = myReader.ReadLine();
            Console.WriteLine(line);
        }
    }
    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);
    }
}

Console.ReadLine();

这将保证StreamReader已关闭,但是以更加自我的C#方式执行此操作。 StreamReader IDisposable.Dispose实施将关闭流

答案 1 :(得分:5)

在try之前声明你的变量:

StreamReader myReader = null;

等。然后在try块中设置它们。

答案 2 :(得分:2)

您需要在StreamReader / try / catch块之外声明finally个实例。

static void Main(string[] args)
{
    using (StreamReader myReader = null)
    {
    try
    {
        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();
}

答案 3 :(得分:1)

只需在try块外定义myReader,最后在调用close

之前检查null
StreamReader myReader = null;
try
{
    myReader = new StreamReader("c:\\j\\MyFile1.txt");
    //.....

在最后一块

finally
{ 
    // Performs the operations that should be accomplished for eg closing the connections, file, database
    if(myReader != null)
        myReader.Close();
}

答案 4 :(得分:1)

这样做

StreamReader myReader = null;
try
        {

           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
        { 
            // Performs the operations that should be accomplished for eg closing the connections, file, database
            if(myReader !=null)
            myReader.Close();

        }



        Console.ReadLine();


    }
}