System.Threading.ThreadAbortException

.NET Exception Handling – System.IO.FileNotFoundException

Continuing along through the .NET Exception Handling series, today we’re going to examine the ever-popular System.IO.FileNotFoundException. The System.IO.FileNotFoundException is common because, as the name suggests, it primarily rears its head when attempting to access a file that doesn’t exist.

In this article, we’ll dive into where System.IO.FileNotFoundException fits into the .NET exception hierarchy, look at when System.IO.FileNotFoundExceptions typically appear, and see how to handle them if you run into one yourself. Let’s get crackin’!

The Technical Rundown

When Should You Use It?

The most common method of accidentally throwing System.IO.FileNotFoundException is when manipulating a file that doesn’t exist, either due to an incorrect path or otherwise. For example, let’s first take a simple code snippet where we’ve defined two methods, WriteLineToFile() and ReadLineFromFile(), both of which perform the function their name describes. We’ve also ensured we aren’t producing any exceptions by using a try-catch block and catching FileNotFoundExceptions:

Since we’re taking advantage of the using code block within both our methods, in which to use our StreamWriterand StreamReader objects, respectively, we don’t need to worry about calling Flush() or Dispose() methods for our reader/writer; .NET does this for us at the end of our using block. Therefore, the meat is simply in creating a new FileStream for the passed in fileName (names.txt in this case), attaching it to our respective reader or writer, then writing a new passed in line value or reading the first line of the existing file. In both cases, we output the line to the console.

As expected, our console output confirms that we did write our new line, and then in the call to ReadLineFromFile(), we were able to retrieve the line:

Now, what happens if we add a second call to ReadLineFromFile() within our Main() method, but we pass an invalid file name to it? Let’s keep everything else we have in the previous code snippet, but our Main() method block now looks like this:

As we might’ve expected, running our code now behaves fine, up until it hits that second call to ReadLineFromFile(). Since the invalid.txt file doesn’t exist, we catch a System.IO.FileNotFoundException:

As it happens, the solution in this case is rather simple. While arguably not a smart practice in production code, we can change the System.IO.FileMode in which our FileStream opens our file within the ReadLineFromFile() method. Originally, we were using FileMode.Open, which simply attempts to open the specified file path, and if it doesn’t exist, we throw a System.IO.FileNotFoundException. Instead, if we want to avoid a System.IO.FileNotFoundException in this case, we can change it to FileMode.OpenOrCreate, like so:

While this isn’t the best long-term solution (we still produce a console output with no line text to pass to it), at the least we’re no longer producing our System.IO.FileNotFoundException.

To get the most out of your own applications and to fully manage any and all .NET Exceptions, check out the Airbrake .NET Bug Handler, offering real-time alerts and instantaneous insight into what went wrong with your .NET code, along with built-in support for a variety of popular development integrations including: JIRA, GitHub, Bitbucket, and much more.