【问题标题】:Return string from catch System.Net.Sockets.SocketException从 catch System.Net.Sockets.SocketException 返回字符串
【发布时间】:2019-05-20 16:16:02
【问题描述】:

我有一个按钮,可以将主机名列表转换为文本框中匹配的 IP 地址。我如何才能为文本框真正不知道的主机名返回字符串“No host is known”?

我使用 try-catch 块来捕获 System.Net.Sockets.SocketException。但据我所知,catch 块不能返回任何字符串值。所以,我通常通过输出一个消息框来捕获异常。但这一次,我只想让它在指定的文本框中显示一个字符串。这是我尝试过的代码:-

private void btnConvertHosttoIP_Click(object sender, Eventrgs e)
{
    try
    {
        string ips = null;
        List<string> ipList = new List<string>();
        string[] hostList = Regex.Split(txtHost.Text, "\r\n");
        foreach (var h in hostList)
        {
            // Check DNS
            if (h.Contains(".xxxx.com"))
            {
                hostName = h;
            }
            else
            {
                string code = txtHost.Text.Substring(0, 3);
                if (code == "ABC" || code == "CDE")
                    hostName = h + ".ap.xxx.com";
                else
                    hostName = "Unknown domain name";
            }

            IPHostEntry host = Dns.GetHostEntry(hostName);
            IPAddress[] ipaddr = host.AddressList;

            // Loop through the IP Address array and add the IP address to Listbox
            foreach (IPAddress addr in ipaddr)
            {
                if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    ipList.Add(addr.ToString());
                }
            }
        }
        foreach (var ip in ipList)
        {
            ips += ip + Environment.NewLine;
        }
        txtIP.Text = ips;
    }
    catch (System.Net.Sockets.SocketException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我希望未知主机仅显示在文本框中,而不是作为例外。是否可以或有其他建议?

【问题讨论】:

  • “Catch 块不能返回字符串”说起来很奇怪——任何代码块都可以随时返回任何东西。编译器是否接受它取决于您所在方法的返回类型。您不能从声明为返回字符串以外的其他内容的方法中返回字符串
  • 请贴出这段代码所在的整个方法(public blah Methodname(args) { ... }
  • 这个方法的返回类型(如果有的话)是什么 - 我们无法通过部分代码 sn-p 发布
  • 为什么不在 try catch 块中放一个 try catch 呢?
  • @CaiusJard 我已经在它的方法中编辑了代码。

标签: c# winforms ip-address hostname


【解决方案1】:

您可以如下修改您的代码。

try
{
    string ips = null;
    List<string> ipList = new List<string>();
    string[] hostList = Regex.Split(txtHost.Text, "\r\n");
    foreach (var h in hostList)
    {
        // Check DNS
        if (h.Contains(".xxxx.com"))
        {
            hostName = h;
        }
        else
        {
            string code = txtHost.Text.Substring(0, 3);
            if (code == "ABC" || code == "CDE")
                hostName = h + ".ap.xxx.com";
            else
                hostName = "Unknown domain name";
        }
        try
       {
        IPHostEntry host = Dns.GetHostEntry(hostName);
        IPAddress[] ipaddr = host.AddressList;

        // Loop through the IP Address array and add the IP address to Listbox
        foreach (IPAddress addr in ipaddr)
        {
            if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                ipList.Add(addr.ToString());
            }
        }
      } 
      catch (System.Net.Sockets.SocketException ex)
     {
         ipList.Add("Invalid Host");
     }
    }
    foreach (var ip in ipList)
    {
        ips += ip + Environment.NewLine;
    }
    txtIP.Text = ips;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

【讨论】:

  • 它对我很有用。我不知道我怎么会不知道这样说。简单易懂。非常感谢。
猜你喜欢
  • 2015-07-07
  • 2018-06-27
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 2021-02-03
  • 2016-04-13
  • 2015-04-30
  • 1970-01-01
相关资源
最近更新 更多