C# IOException Type: File Not Found, Directory Not Found

This C# article covers the IOException type. IOException is a base class for exceptions such as FileNotFoundException.

IOException. File handling often causes errors.

The .NET Framework throws exceptions. In this situation these are derived from the IOException base class. For example, FileNotFoundException can be treated as an IOException.

 

Example. The IOException type allows you to create more general catch clauses. In this program, we cause a FileNotFoundException to be thrown when the file does not exist. If for any reason the file cannot be opened, we get an IOException.

FileNotFoundException

 

Tip: When we catch IOException, we do not need to detect other file-handling exceptions if they occur.

C# program that handles IOException

using System;
using System.IO;

class Program
{
    static void Main()
    {
	try
	{
	    File.Open("C:\\nope.txt", FileMode.Open);
	}
	catch (IOException)
	{
	    Console.WriteLine("IO");
	}
    }
}

Output

IO

 

Example 2. Next we consider the catch construct in more depth. In this program, we detect the possible FileNotFoundException with a separate catch block. The IOException block is only entered if another file handling exception is encountered.

Catch

 

Tip: The exception types should be ordered from most derived to least derived. FileNotFoundException is more derived than IOException.

C# program that handles FileNotFoundException

using System;
using System.IO;

class Program
{
    static void Main()
    {
	try
	{
	    File.Open("C:\\nope.txt", FileMode.Open);
	}
	catch (FileNotFoundException)
	{
	    Console.WriteLine("Not found");
	}
	catch (IOException)
	{
	    Console.WriteLine("IO");
	}
    }
}

Output

Not found

 

Discussion. I opened mscorlib.dll in IL Disassembler to find more about IOException. I browsed to FileNotFoundException and determined it is derived from IOException. It has the "extends System.IO.IOException" decoration.

IL Disassembler

 

 

Also, as with any derived class, you can use casts on IOException to determine its most derived type. You can use the is-cast and the as-cast. The GetType method will return the most derived type as well.

IsAsGetType

 

DirectoryNotFoundException. What could cause the DirectoryNotFoundException in a C# program? DirectoryNotFoundException is a type of file handling exception. It is caused by a missing directory. We further explore this situation.

Directory

 

Example: In this example, we call the Directory.GetDirectories method on a directory that does not exist on the computer.

Thus: An exception is thrown and the program terminates. And no winning lottery tickets are acquired.

C# program that causes DirectoryNotFoundException

using System.IO;

class Program
{
    static void Main()
    {
	Directory.GetDirectories("C:\\lottery-numbers\\");
    }
}

Output

Unhandled Exception: System.IO.DirectoryNotFoundException:
    Could not find a part of the path 'C:\lottery-numbers\'....

Possible fixes for DirectoryNotFoundException depend on the problem. If the software simply requires a certain directory, you could just create that directory manually. Or you could add a method that creates it.

Also, if a serious configuration error exists, a program can display a message requesting it be reinstalled. If the error is more serious, this may be better option, and less frustrating for the user in the long-run.

 

Summary. IOException serves as the base class for file handling exceptions. It is a useful abstraction for checking all such exceptions, not just specific ones. It represents a subset of possible exceptions.