Exception Class and Properties

Microsoft Silverlight will reach end of support after October 2021. Learn more.

The Exception class is the base class from which exceptions inherit. Most exception objects are instances of some derived class ofException, but you can throw any object that derives from the Object class as an exception. Note that not all languages support throwing and catching objects that do not derive from Exception. In almost all cases, it is recommended to throw and catch only Exception objects.

Exception Class Properties

The Exception class has several properties that make understanding an exception easier. These properties include:

  • The StackTrace property.

    This property contains a stack trace that can be used to determine where an error occurred. The stack trace includes the source file name and program line number if debugging information is available.

  • The InnerException property.

    This property can be used to create and preserve a series of exceptions during exception handling. You can use this property to create a new exception that contains previously caught exceptions. The original exception can be captured by the second exception in the InnerException property, allowing code that handles the second exception to examine the additional information.

    To improve the caller's ability to determine the reason an exception is thrown, it is sometimes desirable for a method to catch an exception thrown by a helper routine and then throw an exception more indicative of the error that has occurred. A new and more meaningful exception can be created, where the inner exception reference can be set to the original exception. This more meaningful exception can then be thrown to the caller. Note that with this functionality, you can create a series of linked exceptions that ends with the exception that was thrown first.

  • The Message property.

    This property provides details about the cause of an exception. The message is in the language specified by the Thread.CurrentUICulture property of the thread that throws the exception.

  • The Data property

    This property is an IDictionary that can hold arbitrary data in key-value pairs.

Most of the classes that inherit from Exception do not implement additional members or provide additional functionality; they simply inherit from Exception. Therefore, the most important information for an exception can be found in the hierarchy of exceptions, the exception name, and the information contained in the exception.

See Also

Other Resources