1

I have a button that should convert a list of hostnames to the matching IP addresses in a textbox. How can I just return string "No host is known" for the hostname that is really not known to the textbox?

I use try-catch block in which I catch System.Net.Sockets.SocketException. But as far that I know, catch block cannot return any string value. So, I usually catch the exception by output a messagebox. But this time, I just want it to display a string in the specified textbox. This is the code that I've tried:-

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

I want the unknown host to be displayed in the textbox only not as an exception. Is it possible or any other suggestions?

HNA
  • 107
  • 2
  • 14
  • "Catch block cannot return a string" is an odd thing to say - any block of code can return anything at any time. Whether the compiler accepts it or not depends on what the return type is of the method you're in. You cannot return a string from a method declared to return something other than a string – Caius Jard Dec 20 '18 at 06:50
  • Please post the entire method that this code is in (`public blah Methodname(args) { ... }` ) – Caius Jard Dec 20 '18 at 06:51
  • 1
    what is the return type (if any) of this method - we can't tell by the partial code snippet posted – jazb Dec 20 '18 at 06:51
  • 1
    Why don't you put a try catch inside the try catch block? – Sesen Dec 20 '18 at 07:10
  • @CaiusJard I have edited the code in its method. – HNA Dec 20 '18 at 07:15

1 Answers1

2

You can modify your code as below.

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);
}
  • It worked for me like a charm. I don't know how can I not have idea to put it this way. Simple and understandable. Thank you so much. – HNA Dec 20 '18 at 07:24