#902 – Data Available to the Handler of a Wrapped Exception

There are cases when you handle an exception by wrapping the original exception within a new exception that you then throw.  The original exception is wrapped by storing it in the InnerException property of the new exception.

When a handler further up catches the new exception, it will see the following:

  • StackTrace property shows the calling sequence only down to the method that created the new exception (the handler that caught the lower-level exception)
  • The original (wrapped) exception is accessible through the InnerException property

For example, suppose that:

  • Main method calls DoDogStuff method, which calls Dog.Bark method
  • Dog.Bark throws a DogBarkException
  • DoDogStuff catches DogBarkException, wraps the exception in an ApplicationException and throws the new exception

A handler in Main can then catch the ApplicationException and will see:

  • StackTrace of wrapped exception shows – Main called DoDogStuff
  • InnerException contains the original DogBarkException
  • StackTrace of inner exception shows – DoDogStuff called Dog.Bark

902-001

902-002

Advertisement