locked
using StreamReader ... not able to catch exception of FileNotFoundException RRS feed

  • Question

  • In the below code, I am using "using" for the StreamReader, and try to open a file that does not exist. I have two questions

    1) I am not able to catch the exception of the file not existing, although I have a catch for it

    2) Since I am using "using", the runtime should do the dispose and cleanup, I don't need to add it here. But since I am not able to catch the exception, will this still apply?

    using (StreamReader streamReader = new StreamReader(@"C:\FileDoesNotExist.txt"))
            {
                try
                {
                    string line = "";
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
                catch (System.IO.FileNotFoundException) //not able to catch the exception?
                {
                    Console.WriteLine("System.IO.FileNotFoundException");
                }
                catch (Exception)
                {
                    Console.WriteLine("Exception");
                }
            }






    Wednesday, April 22, 2015 9:31 AM

Answers

  • To following code please examine..

    try
    {	        
    	using (StreamReader streamReader = new StreamReader(@"C:\FileDoesNotExist.txt"))
            {
                    string line = "";
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
    }
    catch (System.IO.FileNotFoundException) //not able to catch the exception?
    {
       Console.WriteLine("System.IO.FileNotFoundException");
    }
    catch (Exception)
    {
       Console.WriteLine("Exception");
    }
           


    oguzkurtcuoglu.com


    Wednesday, April 22, 2015 9:37 AM
  • >>1) I am not able to catch the exception of the file not existing, although I have a catch for it

    You need to put a try/catch block around the call that creates the StreamReader as suggested by Oğuz KURTCUOĞLU.

    >>2) Since I am using "using", the runtime should do the dispose and cleanup, I don't need to add it here. But since I am not able to catch the exception, will this still apply?

    Yes. Even if an unhandled exception occurs within a using statement the IDisposable will still be disposed off as expected so you don't have to anything.


    Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Wednesday, April 22, 2015 10:58 AM

All replies

  • To following code please examine..

    try
    {	        
    	using (StreamReader streamReader = new StreamReader(@"C:\FileDoesNotExist.txt"))
            {
                    string line = "";
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
    }
    catch (System.IO.FileNotFoundException) //not able to catch the exception?
    {
       Console.WriteLine("System.IO.FileNotFoundException");
    }
    catch (Exception)
    {
       Console.WriteLine("Exception");
    }
           


    oguzkurtcuoglu.com


    Wednesday, April 22, 2015 9:37 AM
  • >>1) I am not able to catch the exception of the file not existing, although I have a catch for it

    You need to put a try/catch block around the call that creates the StreamReader as suggested by Oğuz KURTCUOĞLU.

    >>2) Since I am using "using", the runtime should do the dispose and cleanup, I don't need to add it here. But since I am not able to catch the exception, will this still apply?

    Yes. Even if an unhandled exception occurs within a using statement the IDisposable will still be disposed off as expected so you don't have to anything.


    Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Wednesday, April 22, 2015 10:58 AM
  • The C# compiler emits IL that is the equivalent try catch block when it encounters the using. It almost certainly looks like the compiler behavior could be changed to include any user code catch blocks while it emits it. That would have certainly helped. 

    Thanks




    Wednesday, April 22, 2015 3:33 PM