IO, exceptions. File handling often causes errors. The .NET Framework throws exceptions. In this situation these are derived from the IOException base class.
Derived exceptions. For example, FileNotFoundException can be treated as an IOException. The IOException type allows you to create more general catch clauses.
Example. In this program, we cause a FileNotFoundException to be thrown when the file does not exist. If the file cannot be opened, we get an IOException.
Tip When we catch IOException, we do not need to detect other file-handling exceptions if they occur.
using System;
using System.IO;
class Program
{
static void Main()
{
try
{
File.Open("C:\\nope.txt", FileMode.Open);
}
catch (IOException)
{
Console.WriteLine("IO");
}
}
}IO
Example 2. We detect the possible FileNotFoundException with a separate catch block. The IOException block is only entered if another file handling exception is encountered.
Tip The exception types should be ordered from most derived to least derived. FileNotFoundException is more derived than IOException.
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");
}
}
}Not found
DirectoryNotFoundException. What could cause the DirectoryNotFoundException? DirectoryNotFoundException is a type of file handling exception. It is caused by a missing directory.
Detail 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.
Detail If the software requires a certain directory, you could create that directory manually. Or you could add a method that creates it.
using System.IO;
class Program
{
static void Main()
{
Directory.GetDirectories("C:\\lottery-numbers\\");
}
}Unhandled Exception: System.IO.DirectoryNotFoundException:
Could not find a part of the path 'C:\lottery-numbers\'....
Discussion. I opened mscorlib.dll in IL Disassembler to find more about IOException. I browsed to FileNotFoundException and determined it is derived from IOException.
Also As with any derived class, you can use casts on IOException to determine its most derived type.
Detail You can use the is-cast and the as-cast. The GetType method will return the most derived type as well.
Summary. IOException serves as the base class for file handling exceptions. It is a useful abstraction for checking all such exceptions. It represents a subset of possible exceptions.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 11, 2021 (edit).