【问题标题】:what is the difference between TRY....CATCH and TRY.....Finally? [duplicate]TRY....CATCH 和 TRY....Finally 有什么区别? [复制]
【发布时间】:2013-11-09 08:27:57
【问题描述】:

try...catch 和 try....finally 的区别?在 asp.net(C#) 中

就像当我想捕获像 1/0 这样的错误时,我将代码放入 try 块中,并将异常对象放入像 response.write("ERROR:"+ ex.Message) 这样的捕获块中,但顾问告诉我它不是总是把 catch 放在一个好习惯,它吸收错误而不通知?????嗯嗯?但它是通过 ex.Message 完成的,那为什么呢? 什么尝试....最后呢?我知道它是用来释放资源的,但是如果无法捕获异常,TRY 有什么用?

【问题讨论】:

标签: c# asp.net .net clr


【解决方案1】:

finally 块中包含的所有内容都会确保执行,并且至少在这两种具体情况下很有用:

  • 有时你决定在你的 try 块中间调用return 并返回给调用者:finally 在这里简化释放资源的过程,你不必编写一些特定的代码直接去方法结束。
  • 有时您想让异常上升(通过不捕获它)并且可能在更高级别被捕获(例如,因为它不是正确处理它的合适位置)。再次finally 确保您的资源被释放并且异常继续其路径。

也许您可以将finally 视为一种工具,帮助开发人员以更少的努力完成他们必须做的事情。另一方面,catch 专门用于处理错误。

这两个关键字都专用于流量控制,但它们的用途不同,它们可以单独使用(而且经常如此!)。这取决于您的需求。

【讨论】:

    【解决方案2】:

    尝试/抓住/终于:

    try
    {
        // open some file or connection
        // do some work that could cause exception
    }
    catch(MyException ex)
    {
       // do some exception handling: rethrow with a message, log the error, etc...
       // it is not a good practice to just catch and do nothing (swallow the exception)
    }
    finally
    {
        // do some cleanup to close file/connection
        // guaranteed to run even if an exception happened in try block
        // if there was no finally, and exception happened before cleanup in your try block, file could stay open.
    }
    

    尝试/最终:

    try
    {
        // open some file/connection
        // do some work, where you're not expecting an exception
        // or, you don't want to handle the exception here, rather just let it go to the caller, so no need for a catch
    }
    finally
    {
        // do cleanup, guaranteed to go in this finally block
    }
    

    【讨论】:

    • 因为有人告诉他普通的 catch 是不好的,我们还要注意 catch(Exception ex) 通常比捕获你知道可以抛出的特定异常更糟糕。这可能是他不明白的地方,尽管他的问题听起来像是家庭作业。
    • 完成了,不,这不是干活,我是一名专业的网络工程师,虽然是初学者,但我相信你们很快就会把我带到专家级别,你们摇滚:)
    【解决方案3】:

    无论有没有异常,Final 总是被执行。如果您想绝对确定某些东西已清理干净,这可能会很方便。 Example:

    void ReadFile(int index)
    {
        // To run this code, substitute a valid path from your local machine 
        string path = @"c:\users\public\test.txt";
        System.IO.StreamReader file = new System.IO.StreamReader(path);
        char[] buffer = new char[10];
        try
        {
            file.ReadBlock(buffer, index, buffer.Length);
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine("Error reading from {0}. Message = {1}", path, e.Message);
        }
    
        finally
        {
            if (file != null)
            {
                file.Close();
            }
        }
        // Do something with buffer...
    }
    

    如果你没有finally,那么如果发生错误,文件可能无法正确关闭。无论是否发生错误,您都希望在完成后关闭文件。

    考虑替代方案:

    void ReadFile(int index)
    {
        // To run this code, substitute a valid path from your local machine 
        string path = @"c:\users\public\test.txt";
        System.IO.StreamReader file = new System.IO.StreamReader(path);
        char[] buffer = new char[10];
        try
        {
            file.ReadBlock(buffer, index, buffer.Length);
            file.Close();
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine("Error reading from {0}. Message = {1}", path, e.Message);
        }
    }
    

    如果您在ReadBlock 上出错,该文件将无法正确关闭。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      相关资源
      最近更新 更多