try语句中的问题

发布于 2024-07-19 04:31:04 字数 874 浏览 6 评论 0原文

这是我用来设置 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{} 语句之后,即使我捕获了异常,代码仍继续遵循 Final{},为什么会发生这种情况? 我希望它在消息框之后退出该函数。我尝试使用“return”,但它仍然继续遵循finally{}块。

This is the code I use to setup my TCP server

    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);
        }
    }

If you call Bind on the same destination you'll get an exception,because the port is already in use,so do I get that exception when I call that function two times.

Problem - After Catch{} statement the code continues to follow the Finally{} even though I caught the exception,why that happens?
I want it to exit the function after the messagebox.I tried with "return",but it still continues to follow the finally{} block.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我的奇迹 2024-07-26 04:31:04

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

The finally block always executes, regardless if an exception was thrown or the method was exiting from within the try/catch block.

通知家属抬走 2024-07-26 04:31:04

Final 块是放置无论 try 块成功还是失败都必须运行的代码的位置。 您可以在其中放置可能会处置对象等的“清理”代码。

因此,无论发生什么情况,该代码都会运行,这是设计使然。 如果您只想在 Bind 良好时运行该代码,也许您需要将该代码移至 Try 块中。

查看此页面...

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

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

接下来是一个 try/catch/finally 示例(摘自 Jeffery Richter 的 CLR via C#,这应该是您的必读内容)...

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();
}

The Finally block is where you put code that MUST run regardless of whether the try block succeeds or fails. It's where you'd put "clean up" code that might dispose objects etc.

So, it's by-design that this code runs regardless of what happened. Perhaps you need to move that code into the Try block if you only want it running when the Bind is good.

Check out this page...

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

... for details on how this works.

A sample try/catch/finally follows (taken from Jeffery Richter's CLR via C# which should be on your required reading)...

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();
}
〆一缕阳光ご 2024-07-26 04:31:04

正如其他人指出的那样,无论抛出异常,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...
    }

As others have pointed out, the finally block will always occur regardless of an exception being thrown.

Change your code to

    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...
    }
铁轨上的流浪者 2024-07-26 04:31:04

finally 的整体思想是它总是会运行——无论有没有异常。 用它来进行清理等。

The whole idea with finally is that it will always run - exceptions or no exceptions. Use it for clean-up etc.

我一直都在从未离去 2024-07-26 04:31:04

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

听起来你在“最后”中的台词可能属于尝试?

Finally blocks are always executed. That is the whole point of finally.

It sounds like the lines you have in the 'finally' maybe belong in the try?

鹿港小镇 2024-07-26 04:31:04

finally 总是被执行。

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

finally is always executed.

Only the code that exists after the line of code that throwed the exception will not be executed, until a catch exception is encountered that can catch the exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文