So far you've been using the exception as a sentinel—that is, the presence of the exception signals the errors—but you haven't touched or examined the
Exception object itself. The
System.Exception class provides a number of useful methods and properties.
Message property provides information about the exception, such as why it was thrown. The
Message property is read-only; the code throwing the exception can pass in the message as an argument to the exception constructor, but the
Message property cannot be modified by any method once set in the constructor.
HelpLink property provides a link to a help file associated with the exception. This property is read/write. In Example 16-6, the
Exception.HelpLink property is set and retrieved to provide information to the user about the
DivideByZeroException. It is generally a good idea to provide a help file link for any exceptions you create so that the user can learn how to correct the exceptional circumstance.
StackTrace property is set by the CLR. This property is used to provide a stack trace for the error statement. A stack trace is used to display the call stack: the series of method calls that lead to the method in which the exception was thrown.
Keep in mind that although a stack trace is useful to a developer tracking down an error it's probably not useful to an end user. When you're using a stack trace, consider who's going to see it.
Example 16-6. The Exception class ...