在 .NET 中处理 I/O 错误

除了可以在任何方法调用中引发的异常(例如当系统受到压力时的 OutOfMemoryException 或由于程序员错误导致的 NullReferenceException),.NET 文件系统方法还可以引发以下异常:

将错误代码映射到异常

由于文件系统是操作系统资源,因此 .NET Core 和 .NET Framework 中的 I/O 方法会封装对基础操作系统的调用。 当作系统执行的代码中出现 I/O 错误时,作系统会将错误信息返回到 .NET I/O 方法。 然后,该方法会将错误信息(通常以错误代码的形式)转换为 .NET 异常类型。 在大多数情况下,它通过将错误代码直接转换为其相应的异常类型来执行此作;它不基于方法调用的上下文执行错误的任何特殊映射。

例如,在 Windows操作系统上,返回错误代码ERROR_FILE_NOT_FOUND(或0x02)的方法调用映射到FileNotFoundException,错误代码ERROR_PATH_NOT_FOUND(或0x03)映射到DirectoryNotFoundException

但是,作系统返回特定错误代码的精确条件通常未记录或记录不当。 因此,可能会出现意外的异常。 例如,由于您正在使用目录而不是文件,因此可以预期,如果向DirectoryInfo构造函数提供无效的目录路径,会抛出一个DirectoryNotFoundException。 但是,它也可能引发 FileNotFoundException

I/O操作中的异常处理

由于这种对操作系统的依赖,相同的异常情况(比如示例中发生的找不到目录错误)可能使 I/O 方法抛出整个 I/O 异常类中的任意一个。 这意味着,调用 I/O API 时,代码应准备好处理大部分或全部这些异常,如下表所示:

异常类型 .NET Core/.NET 5+ .NET 框架
IOException 是的 是的
FileNotFoundException 是的 是的
DirectoryNotFoundException 是的 是的
DriveNotFoundException 是的 是的
PathTooLongException 是的 是的
OperationCanceledException 是的 是的
UnauthorizedAccessException 是的 是的
ArgumentException .NET Core 2.0 及更早版本 是的
NotSupportedException 是的
SecurityException 仅受限的信任

处理 IOException

作为 System.IO 命名空间中异常的基类,当任何错误代码未映射到预定义的异常类型时,也将引发 IOException。 这意味着异常可以由任何 I/O 操作引发。

重要

由于 IOException 是命名空间中 System.IO 其他异常类型的基类,因此在处理其他 I/O 相关异常后,应在块中 catch 处理。

此外,从 .NET Core 2.1 开始,路径正确性的验证检查(例如,确保路径中不存在无效字符)已被取消,运行时将引发映射自操作系统错误代码的异常,而不是来自其自身验证代码的异常。 在这种情况下最可能引发的异常是一种 IOException,但也可能引发其他任何类型的异常。

请注意,在异常处理代码中,应始终最后处理IOException。 否则,因为它是所有其他 IO 异常的基类,将不会评估派生类的 catch 块。

IOException 情况下,可以从 IOException.HResult 属性获取更多错误信息。 若要将 HResult 值转换为 Win32 错误代码,请去除 32 位值的上限 16 位。 下表列出了可能包含在 IOException 中的错误代码。

HResult 恒定 DESCRIPTION
错误_共享冲突 (ERROR_SHARING_VIOLATION) 32 文件名丢失,或者文件或目录正在被使用。
ERROR_FILE_EXISTS 80 该文件已存在。
参数无效错误 87 提供给方法的参数无效。
错误_已存在 183 文件或目录已存在。

可以在 catch 语句中使用 When 子句处理这些语句,如以下示例所示。

using System;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
        var sw = OpenStream(@".\textfile.txt");
        if (sw is null)
            return;
        sw.WriteLine("This is the first line.");
        sw.WriteLine("This is the second line.");
        sw.Close();
    }

    static StreamWriter? OpenStream(string path)
    {
        if (path is null)
        {
            Console.WriteLine("You did not supply a file path.");
            return null;
        }

        try
        {
            var fs = new FileStream(path, FileMode.CreateNew);
            return new StreamWriter(fs);
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("The file or directory cannot be found.");
        }
        catch (DirectoryNotFoundException)
        {
            Console.WriteLine("The file or directory cannot be found.");
        }
        catch (DriveNotFoundException)
        {
            Console.WriteLine("The drive specified in 'path' is invalid.");
        }
        catch (PathTooLongException)
        {
            Console.WriteLine("'path' exceeds the maximum supported path length.");
        }
        catch (UnauthorizedAccessException)
        {
            Console.WriteLine("You do not have permission to create this file.");
        }
        catch (IOException e) when ((e.HResult & 0x0000FFFF) == 32)
        {
            Console.WriteLine("There is a sharing violation.");
        }
        catch (IOException e) when ((e.HResult & 0x0000FFFF) == 80)
        {
            Console.WriteLine("The file already exists.");
        }
        catch (IOException e)
        {
            Console.WriteLine($"An exception occurred:\nError code: " +
                              $"{e.HResult & 0x0000FFFF}\nMessage: {e.Message}");
        }
        return null;
    }
}
Imports System.IO

Module Program
    Sub Main(args As String())
        Dim sw = OpenStream(".\textfile.txt")
        If sw Is Nothing Then Return

        sw.WriteLine("This is the first line.")
        sw.WriteLine("This is the second line.")
        sw.Close()
    End Sub

    Function OpenStream(path As String) As StreamWriter
        If path Is Nothing Then
            Console.WriteLine("You did not supply a file path.")
            Return Nothing
        End If

        Try
            Dim fs As New FileStream(path, FileMode.CreateNew)
            Return New StreamWriter(fs)
        Catch e As FileNotFoundException
            Console.WriteLine("The file or directory cannot be found.")
        Catch e As DirectoryNotFoundException
            Console.WriteLine("The file or directory cannot be found.")
        Catch e As DriveNotFoundException
            Console.WriteLine("The drive specified in 'path' is invalid.")
        Catch e As PathTooLongException
            Console.WriteLine("'path' exceeds the maximum supported path length.")
        Catch e As UnauthorizedAccessException
            Console.WriteLine("You do not have permission to create this file.")
        Catch e As IOException When (e.HResult And &h0000FFFF) = 32
            Console.WriteLine("There is a sharing violation.")
        Catch e As IOException When (e.HResult And &h0000FFFF) = 80
            Console.WriteLine("The file already exists.")
        Catch e As IOException
            Console.WriteLine($"An exception occurred:{vbCrLf}Error code: " +
                              $"{e.HResult And &h0000FFFF}{vbCrLf}Message: {e.Message}")
        End Try
        Return Nothing
    End Function
End Module

另请参阅