DevFreak
DevFreak

Reputation: 103

variable scope inside try catch block

I'm getting an error while trying to close my file in the Finally code block:

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

Upvotes: 0

Views: 4064

Answers (5)

Cam Bruce
Cam Bruce

Reputation: 5689

You need to declare your StreamReader instance outside the try/catch/finally blocks.

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

Upvotes: 2

Ehsan
Ehsan

Reputation: 32661

do it like this

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();


    }
}

Upvotes: 1

Ryan
Ryan

Reputation: 257

Declare your variables before the try:

StreamReader myReader = null;

etc. Then set them in the try block.

Upvotes: 5

Habib
Habib

Reputation: 223187

just define myReader outside your try block and in finally block check for null before calling close

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

In Finally block

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

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564333

You would need to declare your StreamReader above the try.

That being said, I would recommend using the using statement instead of try/finally in this case, as it's designed specifically for resource cleanup.

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();

This will guarantee that the StreamReader is closed, but do so in a more idomatic C# way. StreamReader's IDisposable.Dispose implementation will close the stream

Upvotes: 6

Related Questions