[Mono-dev] Bug preserving stack trace

Edward Ned Harvey (mono) edward.harvey.mono at clevertrove.com
Wed Jun 22 11:58:31 UTC 2016


I know you don't usually want to catch & throw, because you're not adding new information. If you want to add new information, generally you should throw a new exception and include an InnerException. Here are some examples of situations you would want to catch and rethrow:

https://msdn.microsoft.com/en-us/library/0yd65esw.aspx



I also know you destroy the stack trace if you explicitly name the exception you're rethrowing. In order to preserve the stack, you should throw without any arguments.

https://msdn.microsoft.com/en-us/library/ms182363.aspx



I know the difference between a debug build and release build, particularly with regards to optimizations and inlining.


In a debug build, the following code behaves as expected (prints out the full stack trace) on .NET, but doesn't print out the full stack on mono 4.2.3. I'm pretty sure it's not an expected behavior.

using System;

namespace FunWithRethrow
{
      class MainClass
      {
            public static void Main (string[] args)
            {
                  try {
                        Third ();
                  }
                  catch (Exception e) {
                        Console.WriteLine (e);
                  }
            }
            static void Third()
            {
                  Second ();
            }
            static void Second()
            {
                  try {
                        First();
                  }
                  catch {
                        throw;
                  }
            }
            static void First()
            {
                  throw new Exception ("Something said... Not good...");
            }
      }
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20160622/ed5ce58c/attachment.html>


More information about the Mono-devel-list mailing list