开发者

Does ReadAllLines() function have built in try-catch?

From my understanding, ReadAllLines would open a file, and then return all the lines within that file, and then closes that file/stream. Now I have this piece of code:

try
{
    string[] lines = File.ReadAllLines(path);
}
catch(IOException)
{
    Console.WriteLine("File doesnt exist in : " + path);
}

I did that so that if the file within the path directory doesnt exist, it would throw an error message. My question: is that necessary? Since I dont know how ReadAllLines() was implemented by microsoft, I dont really know if it already had a try catch built-in within the function implementation..

I however, can "guess" that ReadAllLines() would always close the file stream everytime it finishes reading it. that is the reason I did not run the cleanup code that is supposed to be included within the finally{} block.

Can somebody explain/give me confirmation about this? Any help would be appreciated. L开发者_开发问答et me know if the question is not clear. Thank you.


I looked at the source in ILSpy and it looks like it does the following:

[SecuritySafeCritical]
public static string[] ReadAllLines(string path)
{
if (path == null)
{
    throw new ArgumentNullException("path");
}
if (path.Length == 0)
{
    throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
}
return File.InternalReadAllLines(path, Encoding.UTF8);
}

And the method InternalReadAllLines:

private static string[] InternalReadAllLines(string path, Encoding encoding)
{
List<string> list = new List<string>();
using (StreamReader streamReader = new StreamReader(path, encoding))
{
    string item;
    while ((item = streamReader.ReadLine()) != null)
    {
        list.Add(item);
    }
}
return list.ToArray();
}


Reading the documentation of the File.ReadAllLines() method helps - all the exceptions that might be thrown are listed there - this includes IOException and FileNotFoundException.


The documentation here lists a number of exceptions that will be thrown by ReadAllLines in various circumstances. All the exceptions listed would result in not having the file open, so cleanup code in a finally block shouldn't be necessary, but you definitiely want to catch any exceptions that may occur in your context.


Yes it does throw an exception on if not found etc. Just type File.ReadAllLines and intellisense will show you all the exceptions that the method can possibly throw. And yes, it does clean up, you don't need to worry about inner streams etc. if that's you concern.


If the file does not exist at the end of that path, or if the file is locked, etc. then yes, File.ReadAllLines(path) will throw an exception and you are right to assume to use a try catch block.


Yes, the documentation says it throws:

http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx


You can look at the msdn documentation of the method which includes an explanation of which Exceptions will be thrown and under what circumstances:

http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx

Exception ArgumentException
path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

FileNotFoundException
The file specified in path was not found.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜