Skip to content
StephenCleary edited this page Sep 5, 2014 · 1 revision

Overview

There is one static method on the ExceptionHelpers type: PrepareForRethrow.

The purpose of this method is to preserve the original stack trace of the exception so that it is not overwritten when the exception is rethrown.

The return value of this method should always be thrown immediately; that is, every call to this method should look like this:

Exception ex = ...; // get saved exception
throw ExceptionHelpers.PrepareForRethrow(ex);

PrepareForRethrow will use ExceptionDispatchInfo if it is running on a machine with .NET 4.5 installed. If only .NET 4.0 is available, this method will fall back to the undocumented but somewhat common reflection hack of calling Exception.PrepForRemoting. If both of these approaches fail (which may happen in Silverlight or partial-trust scenarios), the original stack trace is added to the Data property of the exception.

API

// Provides helper (non-extension) methods dealing with exceptions.
public static class ExceptionHelpers
{
  // Attempts to prepare the exception for re-throwing by preserving the stack trace. The returned exception should be immediately thrown.
  Exception PrepareForRethrow(Exception exception);
}

Platform Support

The full API is supported on all platforms.