添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
咆哮的青蛙  ·  qsort函数、sort函数 ...·  10 月前    · 
玩篮球的煎鸡蛋  ·  WindowsIdentity.Impers ...·  12 月前    · 
仗义的烈酒  ·  Button ...·  1 年前    · 

除了可能在任何方法调用中引发异常(如系统压力过大导致 OutOfMemoryException 或由于编程器错误导致 NullReferenceException ),.NET 文件系统方法还可能引发以下异常:

  • System.IO.IOException ,所有 System.IO 异常类型的基类。 当出现操作系统的返回代码不直接映射到任何其他异常类型的错误时将引发此异常。
  • System.IO.FileNotFoundException .
  • System.IO.DirectoryNotFoundException .
  • DriveNotFoundException .
  • System.IO.PathTooLongException .
  • System.OperationCanceledException .
  • System.UnauthorizedAccessException .
  • 当 .NET Framework 和 .NET Core 2.0 以及先前版本上的路径字符无效时,将引发 System.ArgumentException
  • 当 .NET Framework 中存在无效冒号时,将引发 System.NotSupportedException
  • 当在受限制的信任中运行的应用程序缺少仅限于 .NET Framework 的所需权限时,将引发 System.Security.SecurityException 。 (完全信任是 .NET Framework 的默认设置。)
  • 将错误代码映射到异常

    由于文件系统为操作系统资源,.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 Framework

    处理 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 返回的常量 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; 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
                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
    
  • 在 .NET 中处理和引发异常
  • 异常处理(任务并行库)
  • 针对异常的最佳做法
  • 如何在 catch 块中使用特定异常
  •