Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can get the offset of the last MSIL instruction from the Exception using System.Diagnostics.StackTrace:</p> <pre><code>// Using System.Diagnostics static void Main(string[] args) { try { ThrowError(); } catch (Exception e) { StackTrace st = new System.Diagnostics.StackTrace(e); string stackTrace = ""; foreach (StackFrame frame in st.GetFrames()) { stackTrace = "at " + frame.GetMethod().Module.Name + "." + frame.GetMethod().ReflectedType.Name + "." + frame.GetMethod().Name + " (IL offset: 0x" + frame.GetILOffset().ToString("x") + ")\n" + stackTrace; } Console.Write(stackTrace); Console.WriteLine("Message: " + e.Message); } Console.ReadLine(); } static void ThrowError() { DateTime myDateTime = new DateTime(); myDateTime = new DateTime(2000, 5555555, 1); // won't work Console.WriteLine(myDateTime.ToString()); } </code></pre> <p>Output:</p> <blockquote> <p>at ConsoleApplicationN.exe.Program.Main (IL offset: 0x7)<br> at ConsoleApplicationN.exe.Program.ThrowError (IL offset: 0x1b)<br> at mscorlib.dll.DateTime..ctor (IL offset: 0x9)<br> at mscorlib.dll.DateTime.DateToTicks (IL offset: 0x61)<br> Message: Year, Month, and Day parameters describe an un-representable DateTime. </p> </blockquote> <p>You can then use <a href="http://www.red-gate.com/products/reflector/" rel="nofollow noreferrer">Reflector</a> or <a href="http://ilspy.net/" rel="nofollow noreferrer">ILSpy</a> to interpret the offset:</p> <pre><code>.method private hidebysig static void ThrowError() cil managed { .maxstack 4 .locals init ( [0] valuetype [mscorlib]System.DateTime myDateTime) L_0000: nop L_0001: ldloca.s myDateTime L_0003: initobj [mscorlib]System.DateTime L_0009: ldloca.s myDateTime L_000b: ldc.i4 0x7d0 L_0010: ldc.i4 0x54c563 L_0015: ldc.i4.1 L_0016: call instance void [mscorlib]System.DateTime::.ctor(int32, int32, int32) L_001b: nop L_001c: ldloca.s myDateTime L_001e: constrained [mscorlib]System.DateTime L_0024: callvirt instance string [mscorlib]System.Object::ToString() L_0029: call void [mscorlib]System.Console::WriteLine(string) L_002e: nop L_002f: ret } </code></pre> <p>You know that the instruction before 0x1b threw the exception. It's easy to find the C# code for that:</p> <pre><code> myDateTime = new DateTime(2000, 5555555, 1); </code></pre> <p>You could map the IL code to your C# code now, but I think the gain would be too little and the effort too big (though there might be a reflector plugin). You should be fine with the IL offset.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload