【问题标题】:problem in try statementtry 语句中的问题
【发布时间】:2010-10-22 19:36:06
【问题描述】:

这是我用来设置我的 TCP 服务器的代码

    internal void Initialize(int port,string IP)
    {
        IPEndPoint _Point = new IPEndPoint(IPAddress.Parse(IP), port);
        Socket _Accpt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try
        {
            _Accpt.Bind(_Point);
        }
        catch (SocketException exc)
        {
            System.Windows.Forms.MessageBox.Show(exc.Message);

        }
        finally
        {
            _Accpt.Listen(2); //Second exception is here after the code continues after the catch block
            _Accpt.BeginAccept(null, 0, new AsyncCallback(Accept), _Accpt);
        }
    }

如果你在同一个目的地调用 Bind,你会得到一个异常,因为端口已经在使用中,所以当我调用该函数两次时,我会得到那个异常。

问题 - 在 Catch{} 语句之后,即使我捕获了异常,代码仍继续遵循 finally{},为什么会发生这种情况? 我希望它在消息框之后退出函数。我尝试使用“return”,但它仍然继续跟随 finally{} 块。

【问题讨论】:

    标签: c# exception-handling try-catch finally


    【解决方案1】:

    正如其他人所指出的,finally 块将始终发生,无论是否引发异常。

    将代码更改为

        try
        {
            _Accpt.Bind(_Point);
            _Accpt.Listen(2); //Second exception is here after the code continues after the catch block
            _Accpt.BeginAccept(null, 0, new AsyncCallback(Accept), _Accpt);
        }
        catch (SocketException exc)
        {
            System.Windows.Forms.MessageBox.Show(exc.Message);
    
        }
        finally
        {
            //Final logging
            //Disposal of initial objects etc...
        }
    

    【讨论】:

      【解决方案2】:

      finally 总是被执行。

      只有在抛出异常的代码行之后存在的代码才会被执行,直到遇到可以捕获异常的catch异常。

      【讨论】:

        【解决方案3】:

        Finally 块是您放置无论 try 块成功还是失败都必须运行的代码的地方。这是您放置可能处理对象等的“清理”代码的地方。

        因此,无论发生什么,此代码都会运行,这是设计使然。如果您只希望在 Bind 良好时运行该代码,则可能需要将该代码移动到 Try 块中。

        看看这个页面...

        http://msdn.microsoft.com/en-us/library/6dekhbbc(VS.80).aspx

        ...有关其工作原理的详细信息。

        随后是一个示例 try/catch/finally(取自 Jeffery Richter 通过 C# 编写的 CLR,这应该是您的必读内容)...

        FileStream fs = null;
        
        try
        {
          fs = new FileStream(...)
        
          // process the data
        
        }
        catch (IOException)
        {
          // inside this catch block is where you put code that recovers
          // from an IOException
        }
        finally
        {
          // make sure the file gets closed
          if (fs != null) fs.Close();
        }
        

        【讨论】:

          【解决方案4】:

          最终块总是被执行。这就是 finally 的重点。

          听起来你在'finally'中的行可能属于try?

          【讨论】:

            【解决方案5】:

            finally 的整个想法是它将始终运行 - 异常或无异常。用于清理等。

            【讨论】:

              【解决方案6】:

              finally 块总是执行,无论是否抛出异常或方法从 try/catch 块中退出。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2019-08-21
                • 1970-01-01
                • 2012-04-03
                • 1970-01-01
                • 2012-11-19
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多