c# - Getting error on Host to IP conversion if I don't have connectivity - Help! :-)

Greetings Experts,

I have the following code:

                IPAddress[] addresslist = Dns.GetHostAddresses("www.google.com");
                foreach (IPAddress theaddress in addresslist)
                {
                    MessageBox.Show(theaddress.ToString());
                }

Which works GREAT.... as long as I actually have network connectivity, but when I don't have network connectivity, as I would suspect, I get an error on the following line:

 IPAddress[] addresslist = Dns.GetHostAddresses("www.google.com");

Which makes sense to me, because the app wouldn't have access to dns without network connectivity...

But.. my question is... how do I handle the error? what I would like to do, in the event of an error would be to:
            theaddress = "1.1.1.1";

So, the Question is, how do I handle the error, and as a result of the error, set theaddress to "1.1.1.1"?

Thanks!
MiShepsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RishadanPortCommented:
try{
   IPAddress[] addresslist = Dns.GetHostAddresses("www.google.com");
}
catch(Exception e){
   //set addresslist to whatever you want here
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MiShepsAuthor Commented:
Nope, didn't work
0
MiShepsAuthor Commented:
scratch that, I just didn't understand that "try" was part of the code, figured it out though, and your solution works well, with a small bit added.

Good help, thanks for the info!

Here is what I ended up with:

                bool SkipServerIPConvert;
 
                // Convert IP for "Other" Server
                // #############################
                SkipServerIPConvert == false;
                try
                {
                    IPAddress[] addresslist = Dns.GetHostAddresses(MyGlobalVariables.OtherServer);
                }
                catch (Exception e)
                {
                    MyGlobalVariables.OtherServer = "1.1.1.1";
                    SkipServerIPConvert = true;
                }
                if (SkipServerIPConvert == false)
                {
                    IPAddress[] addresslist = Dns.GetHostAddresses(MyGlobalVariables.OtherServer);
                    foreach (IPAddress theaddress in addresslist)
                    {
                        MyGlobalVariables.OtherServer = theaddress.ToString();
                    }
                }
                // #############################

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.