#886 – Setting the HelpLink Property when You Throw an Exception

You can use the HelpLink property of an Exception object to point to a web page that might have information that is relevant to the exception that you are throwing.  Exception handling code could then use the property to navigate to a web page.

In the code below, we throw an exception if the caller asks a Dog to bark too many times.

        public void Bark(int numTimes)
        {
            if (numTimes > 12)
            {
                Exception xx = new Exception("Too much barking");
                xx.HelpLink = "http://www.ehow.com/how_4510663_stop-dog-from-barking-much.html";
                throw xx;
            }

            Console.WriteLine(string.Format("{0}: Woof", Name));
        }

In our exception handler, we then navigate to a web page if HelpLink has been set.

            try
            {
                Dog d1 = new Dog("Jack", 15);
                d1.Bark(1000);
            }
            catch (Exception xx)
            {
                Console.WriteLine(xx);
                if (xx.HelpLink != null)
                    Process.Start(xx.HelpLink);
            }

886-001

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #886 – Setting the HelpLink Property when You Throw an Exception

  1. Pingback: Dew Drop – July 12, 2013 (#1,584) | Alvin Ashcraft's Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: