Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly handle exceptions when working with files in C#
    text
    copied!<p>I've read many blogs/articles/book chapters about proper exception handling and still this topic is not clear to me. I will try to illustrate my question with following example. </p> <p>Consider the class method that has following requirements:</p> <ol> <li>receive list of file paths as parameter </li> <li>read the file content for each file or skip if there is any problem trying to do that </li> <li>return list of objects representing file content</li> </ol> <p>So the specs are straightforward and here is how I can start coding:</p> <pre class="lang-cs prettyprint-override"><code> public class FileContent { public string FilePath { get; set; } public byte[] Content { get; set; } public FileContent(string filePath, byte[] content) { this.FilePath = filePath; this.Content = content; } } static List&lt;FileContent&gt; GetFileContents(List&lt;string&gt; paths) { var resultList = new List&lt;FileContent&gt;(); foreach (var path in paths) { // open file pointed by "path" // read file to FileContent object // add FileContent to resultList // close file } return resultList; } </code></pre> <p>Now note that the 2. from the specs says that method should "skip any file which content can't be read for some reason". So there could be many different reasons for this to happen (eg. file not existing, file access denied due to lack of security permissions, file being locked and in use by some other application etc...) but the point is that I should not care what the reason is, I just want to read file's content if possible or skip the file if not. I don't care what the error is...</p> <p>So how to properly implement this method then?</p> <p>OK the first rule of proper exception handling is never catch general Exception. So this code is not good then:</p> <pre class="lang-cs prettyprint-override"><code> static List&lt;FileContent&gt; GetFileContents(List&lt;string&gt; paths) { var resultList = new List&lt;FileContent&gt;(); foreach (var path in paths) { try { using (FileStream stream = File.Open(path, FileMode.Open)) using (BinaryReader reader = new BinaryReader(stream)) { int fileLength = (int)stream.Length; byte[] buffer = new byte[fileLength]; reader.Read(buffer, 0, fileLength); resultList.Add(new FileContent(path, buffer)); } } catch (Exception ex) { // this file can't be read, do nothing... just skip the file } } return resultList; } </code></pre> <p>The next rule of proper exception handlig says: catch only specific exceptions you can handle. Well I do not I care about handling any specific exceptions that can be thrown, I just want to check if file can be read or not. How can I do that in a proper, the best-practice way?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload