代码之家  ›  专栏  ›  技术社区  ›  Morten Christiansen

在执行文件IO时如何正确处理异常

  •  15
  • Morten Christiansen  · 技术社区  · 16 年前

    我经常发现自己以某种方式与文件进行交互,但是在编写代码之后,我总是不确定它到底有多像rubust。问题是,我不完全确定与文件相关的操作如何失败,因此,这是处理预期的最佳方法。

    简单的解决方案似乎只是捕获代码抛出的任何IOExceptions,并给用户一条“不可访问的文件”错误消息,但是否可以获得更细粒度的错误消息。是否有办法确定此类错误(如文件被另一个程序锁定)与由于硬件错误而无法读取的数据之间的差异?

    考虑到以下C代码,您将如何以一种用户友好(尽可能提供信息)的方式处理错误?

    public class IO
    {
       public List<string> ReadFile(string path)
       {
          FileInfo file = new FileInfo(path);
    
          if (!file.Exists)
          {
             throw new FileNotFoundException();
          }
    
          StreamReader reader = file.OpenText();
          List<string> text = new List<string>();
    
          while (!reader.EndOfStream)
          {
             text.Add(reader.ReadLine());
          }
    
          reader.Close();
          reader.Dispose();
          return text;
       }
    
       public void WriteFile(List<string> text, string path)
       {
          FileInfo file = new FileInfo(path);
    
          if (!file.Exists)
          {
             throw new FileNotFoundException();
          }
    
          StreamWriter writer = file.CreateText();
    
          foreach(string line in text)
          {
             writer.WriteLine(line);
          }
    
          writer.Flush();
          writer.Close();
          writer.Dispose();
       }
    }
    
    7 回复  |  直到 16 年前
        1
  •  12
  •   Community CDub    8 年前

    IOException Exception.ToString() Exception

    throw new FileNotFoundException("File not found");

    Scott Dorman using catch vexing

    try {  
        using (StreamReader reader = file.OpenText()) {  
            // Your processing code here  
        }  
    } catch (IOException e) {  
        UI.AlertUserSomehow(e.ToString());  
    }



    try

        2
  •  7
  •   Scott Dorman    16 年前

    using (StreamReader reader = file.OpenText())
    {
       List<string> text = new List<string>();
       while (!reader.EndOfStream)
       {
          text.Add(reader.ReadLine());
       }
    }
    

    StreamReader reader = file.OpenText();
    try
    {
       List<string> text = new List<string>();
       while (!reader.EndOfStream)
       {
          text.Add(reader.ReadLine());
       }
    }
    finally
    {
       if (reader != null)
          ((IDisposable)reader).Dispose();
    }
    

        3
  •  1
  •   Rune    16 年前

        4
  •  1
  •   to StackOverflow    16 年前

    public IList<string> ReadFile(string path)
    {
        List<string> text = new List<string>();
        using(StreamReader reader = new StreamReader(path))
        {
          while (!reader.EndOfStream)      
          {         
             text.Add(reader.ReadLine());      
          }
        }
        return text;
    }
    
        5
  •  0
  •   Brian    16 年前

    MSDN the C# using statement

      using (TextWriter w = File.CreateText("log.txt")) {
         w.WriteLine("This is line one");
         w.WriteLine("This is line two");
      }
      using (TextReader r = File.OpenText("log.txt")) {
         string s;
         while ((s = r.ReadLine()) != null) {
            Console.WriteLine(s);
         }
      }
    
        7
  •  -2
  •   Per Hornshøj-Schierbeck    16 年前

    推荐文章