如何使用try/catch/finally C#高效地管理Stream

Max*_*mus 5 c# exception-handling exception try-catch stream

我最近与一位同事讨论过,他告诉我,我正在不经意地将一个流管理成一个try/catch/block.所以我想知道对你来说什么是好方法.

            try
            {
                StreamReader sr = new StreamReader("TestFile.txt");
                //After that, here an operation of about 30 seconds to fulfill;
            }
            catch (IOException ioex)
            {
                throw new IOException("An error occurred while processing the file.", ioex);
            }
            catch (Exception ex)
            {
                throw new Exception("An generic error ocurred.");
            }
            finally
            {
                if(sr != null){
                    stream.Close();
                    stream = null;
                }
            }
Run Code Online (Sandbox Code Playgroud)

他表示即使使用IOException,也不需要2个Exception.我们只能使用Exception.但我唯一想要的是识别产生异常的位置,因为在打开文件后,将执行大约30秒的操作.

那你会怎么想?我们看到了这个MS示例(http://msdn.microsoft.com/fr-Fr/library/system.io.streamreader.aspx)它更简单但是在性能或干净的代码方面,你发现了一些奇怪的东西?

请你的意见!

-编辑 - - - - - - - - -

好的,我看到了这一点,但我们正在讨论Catch IOException并仅使用Exception.在我看来,就像上面的例子一样,你可以知道错误发生在哪里; 在管理文件或打开文件后的过程中.那是我的第一个问题.现在,您如何看待下面的这一变化.

            try
            {
                using(StreamReader sr = new StreamReader("TestFile.txt"))
                {
                //After that, here an operation of about 30 seconds to fulfill;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("An generic error ocurred.");
            }
            finally
            {
                if(sr != null){
                    stream.Close();
                    stream = null;
                }
            }
Run Code Online (Sandbox Code Playgroud)

-------------------编辑2 ------------------------

最后,我希望这将是我的最终解决方案.非常感谢您的回答.使用更快,更有效,只有一个例外是必要的.

            try
            {
                using (StreamReader stream = sr = new StreamReader("TestFile.txt"))
                {
                    //Operation
                }
            }
            catch (Exception e)
            {
                throw new Exception(String.Format("An error ocurred while executing the data import: {0}", e.Message), e);
            }
Run Code Online (Sandbox Code Playgroud)

如果有任何其他评论,将不胜感激!

Dam*_*ith 11

您可以使用using下面的块,即使发生异常,它也会处理流

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
   // do something with sr
}
Run Code Online (Sandbox Code Playgroud)

如果您要做某事,请捕获例外.如果你无法解决问题,就没有必要抓住它.

如果您无法解决异常,最好让异常冒泡异常并将其捕获.

try
{
    using(StreamReader sr = new StreamReader("TestFile.txt"))
    {
       // your code 
    }
}
catch (IOException ioex)
{
    // do something to fix the problem 
    // log the exception 
}
Run Code Online (Sandbox Code Playgroud)


Tim*_*vis 7

不要捕获异常只是为了立即抛出相同的异常,只是现在只有更少的信息并且缺少实际发生异常的堆栈帧.

如果我遇到类似的东西

catch (Exception ex)
{
   throw new Exception("An generic error ocurred.");
}
Run Code Online (Sandbox Code Playgroud)

在代码审查中,我会失败的审查(不仅仅是语法和拼写错误;-)

至少,您应该将原始异常作为内部异常抛出.

catch (Exception ex)
{
    throw new Exception("A generic error occurred.", ex)
}
Run Code Online (Sandbox Code Playgroud)

但坦率地说,在这个示例代码中它没有添加任何东西,它会更好地完全删除imo.