Home
Map
IOException TypeReview the IOException type. Consider exceptions such as DirectoryNotFoundException.
C#
This page was last reviewed on Dec 11, 2021.
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.
Exception
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.
FileNotFoundException
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.
catch
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.
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.
is
as
GetType
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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 11, 2021 (edit).
Home
Changes
© 2007-2024 Sam Allen.