<p>I thought .Net code gets compiled into MSIL, so I always wondered how do Yellow Screens produce the faulty code.  If it's executing the compiled code, how is the compiler able to produce code from the source files in the error message?</p>

<p>Feel free to edit this question/title, I know it doesn't really make sense.</p>

[{'answer_id': 20202, 'author': 'John Christensen', 'author_id': 1194, 'author_profile': 'https://Stackoverflow.com/users/1194', 'pm_score': 2, 'selected': False, 'text': '<p>I believe the pdb files that are output when you do a debug build contain a reference to the location of the source code files.</p>\n'}, {'answer_id': 20205, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 0, 'selected': False, 'text': '<p>I think this is down to the debug information that can be included with the compiled assemblies..(although I could definately be wrong)</p>\n'}, {'answer_id': 20210, 'author': 'Nick', 'author_id': 1490, 'author_profile': 'https://Stackoverflow.com/users/1490', 'pm_score': 0, 'selected': False, 'text': '<p>I believe the information that maps the source to the MSIL is stored in the PDB file.  If this is not present then that mapping won\'t happen.</p>\n\n<p>It is this lookup that makes an exception such a expensive operation ("exceptions are for exceptional situations").</p>\n'}, {'answer_id': 20219, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 4, 'selected': True, 'text': '<p>A .Net assembly is compiled with metadata about the bytecode included that allows easy decompilation of the code - that\'s how tools like <a href="http://www.red-gate.com/products/reflector/" rel="noreferrer">.Net Reflector</a> work.  The PDB files are debug symbols only - the difference in the Yellow Screen Of Death is that you\'ll get line numbers in the stack trace.</p>\n\n<p>In other words, you\'d get the code, even if the PDB files were missing.</p>\n'}, {'answer_id': 20315, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 3, 'selected': False, 'text': '<p>like this. i\'ve made a few changes, but it\'s pretty close to exactly what ms is doing.</p>\n\n<pre><code>// reverse the stack\nprivate static Stack&lt;Exception&gt; GenerateExceptionStack(Exception exception)\n{\n    var exceptionStack = new Stack&lt;Exception&gt;();\n\n    // create exception stack\n    for (Exception e = exception; e != null; e = e.InnerException)\n    {\n        exceptionStack.Push(e);\n    }\n\n    return exceptionStack;\n}\n\n// render stack\nprivate static string GenerateFormattedStackTrace(Stack&lt;Exception&gt; exceptionStack)\n{\n    StringBuilder trace = new StringBuilder();\n\n    try\n    {\n        // loop through exception stack\n        while (exceptionStack.Count != 0)\n        {\n            trace.Append("\\r\\n");\n\n            // render exception type and message\n            Exception ex = exceptionStack.Pop();\n            trace.Append("[" + ex.GetType().Name);\n            if (!string.IsNullOrEmpty(ex.Message))\n            {\n                trace.Append(":" + ex.Message);\n            }\n            trace.Append("]\\r\\n");\n\n            // Load stack trace\n            StackTrace stackTrace = new StackTrace(ex, true);\n            for (int frame = 0; frame &lt; stackTrace.FrameCount; frame++)\n            {\n                StackFrame stackFrame = stackTrace.GetFrame(frame);\n                MethodBase method = stackFrame.GetMethod();\n                Type declaringType = method.DeclaringType;\n                string declaringNamespace = "";\n\n                // get declaring type information\n                if (declaringType != null)\n                {\n                    declaringNamespace = declaringType.Namespace ?? "";\n                }\n\n                // add namespace\n                if (!string.IsNullOrEmpty(declaringNamespace))\n                {\n                    declaringNamespace += ".";\n                }\n\n                // add method\n                if (declaringType == null)\n                {\n                    trace.Append(" " + method.Name + "(");\n                }\n                else\n                {\n                    trace.Append(" " + declaringNamespace + declaringType.Name + "." + method.Name + "(");\n                }\n\n                // get parameter information\n                ParameterInfo[] parameters = method.GetParameters();\n                for (int paramIndex = 0; paramIndex &lt; parameters.Length; paramIndex++)\n                {\n                    trace.Append(((paramIndex != 0) ? "," : "") + parameters[paramIndex].ParameterType.Name + " " + parameters[paramIndex].Name);\n                }\n                trace.Append(")");\n\n\n                // get information\n                string fileName = stackFrame.GetFileName() ?? "";\n\n                if (!string.IsNullOrEmpty(fileName))\n                {\n                    trace.Append(string.Concat(new object[] { " in ", fileName, ":", stackFrame.GetFileLineNumber() }));\n                }\n                else\n                {\n                    trace.Append(" + " + stackFrame.GetNativeOffset());\n                }\n\n                trace.Append("\\r\\n");\n            }\n        }\n    }\n    catch\n    {\n    }\n\n    if (trace.Length == 0)\n    {\n        trace.Append("[stack trace unavailable]");\n    }\n\n    // return html safe stack trace\n    return HttpUtility.HtmlEncode(trace.ToString()).Replace(Environment.NewLine, "&lt;br&gt;");\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/20198', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/40/']