public static class Program
public static void Main(string[] args)
var mex = ai.InnerException as MovementException;
Console.WriteLine("Original ==> " + mex);
var mex = ai.InnerException as MovementException;
Console.WriteLine("Patched ==> " + mex);
public static void Test(bool simulatePatching)
catch (MovementException mex)
throw new AIException("Could not move to target", mex);
public static void ORIGINAL()
public static void A() { B(); }
public static void B() { C(); }
public static void C() { throw new MovementException("first"); }
public static void PATCHED()
PreserveStackTrace(__ex);
public static Exception Finally1(Exception ex)
var f_Message = ex.GetType().GetField("_message", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
f_Message.SetValue(ex, "second");
public static void Finally2()
public static Exception Finally3(Exception ex)
if (ex is ArgumentNullException)
public static Exception Finally4(Exception ex)
private static void PreserveStackTrace(Exception exception)
var preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
preserveStackTrace.Invoke(exception, null);
public class MovementException : System.Exception
public MovementException(string name) : base(name) { }
public class AIException : System.Exception
public AIException(string name, Exception ex) : base(name, ex) { }