#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

Advertisement