Skip to content

csdotnetExceptions

Illegitimis edited this page May 31, 2017 · 2 revisions
  1. Call stack context From SO. In short, throw usually preserves the stack trace of the original thrown exception, but only if the exception didn't occur in the current stack frame (i.e. method).
    //To preserve the full call stack information, you need to use the following method: 
    private static void PreserveStackTrace(Exception exception) { 
      System.Reflection.MethodInfo preserveStackTrace = typeof(Exception)
        .GetMethod("InternalPreserveStackTrace",  
        System.Reflection.BindingFlags.Instance | .Reflection.BindingFlags.NonPublic); 
      preserveStackTrace.Invoke(exception, null);
    }

ExceptionCallStackTest.cs gist | MSTest raw file

  1. How to decorate an exception: Use the public virtual IDictionary Data { get; } property. Gets a collection of key/value pairs that provide additional user-defined information about the exception. msdn

<<

Clone this wiki locally