
{"id":596,"date":"2010-09-14T22:09:13","date_gmt":"2010-09-15T02:09:13","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=596"},"modified":"2010-09-14T22:09:13","modified_gmt":"2010-09-15T02:09:13","slug":"net-exception-stack-trace-has-no-frames-above-the-catch-point","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=596","title":{"rendered":".NET: Exception stack trace has no frames above the catch point"},"content":{"rendered":"<p>I never thought about it, but it turns out that exception&#8217;s stack trace does not contain the full stack. Instead, it contains only frames <i>between<\/i> the point where the exception was thrown and the point it is caught. This is bad if exception is caught close to the point of throwing and logged:<\/p>\n<div style=\"font-family: Courier New; font-size: 10pt; color: black; background: white;\">\n<p style=\"margin: 0px;\"><span style=\"color: blue;\">using<\/span> System;<\/p>\n<p style=\"margin: 0px;\">&nbsp;<\/p>\n<p style=\"margin: 0px;\"><span style=\"color: blue;\">namespace<\/span> ExceptionTrace<\/p>\n<p style=\"margin: 0px;\">{<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">class<\/span> <span style=\"color: #2b91af;\">Program<\/span><\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">void<\/span> f1()<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; f2();<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; f3();<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin: 0px;\">&nbsp;<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">void<\/span> f2() { f4(); }<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">void<\/span> f3() { f4(); }<\/p>\n<p style=\"margin: 0px;\">&nbsp;<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">void<\/span> f4()<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">try<\/span> { <span style=\"color: blue;\">throw<\/span> <span style=\"color: blue;\">new<\/span> <span style=\"color: #2b91af;\">Exception<\/span>(<span style=\"color: #a31515;\">&quot;Boo!&quot;<\/span>); }<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">catch<\/span> (<span style=\"color: #2b91af;\">Exception<\/span> ex)<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af;\">Console<\/span>.WriteLine(<span style=\"color: #a31515;\">&quot;Problem: &quot;<\/span> + ex); <span style=\"color: green;\">\/\/ stack trace contains only f4()<\/span><\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin: 0px;\">&nbsp;<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">static<\/span> <span style=\"color: blue;\">void<\/span> Main(<span style=\"color: blue;\">string<\/span>[] args)<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">new<\/span> <span style=\"color: #2b91af;\">Program<\/span>().f1();<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin: 0px;\">}<\/p>\n<\/div>\n<p>This prints:<br \/>\n<code><\/p>\n<pre>Problem: System.Exception: Boo!\n   at ExceptionTrace.Program.f4() in C:\\Ivan\\dev\\exp\\ExceptionTrace\\Program.cs:line 18\nProblem: System.Exception: Boo!\n   at ExceptionTrace.Program.f4() in C:\\Ivan\\dev\\exp\\ExceptionTrace\\Program.cs:line 18<\/pre>\n<p><\/code><\/p>\n<p>Note that both stack traces are exactly identical and contain only one frame. The message does not say whether f4() was called from f2() or from f3(). This information can be obtained by calling <code>new StackTrace()<\/code> inside the <code>catch<\/code>, which returns the <i>full<\/i> stack, but this stack trace is not part of the exception.<\/p>\n<p>If we rethrow the exception and let it travel up the call chain, its <code>StackTrace<\/code> will automatically grow to reflect additional frames, but it will never include the frames above the catching block. <a style=\"display:none\" href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=1181663\" rel=\"tag\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I never thought about it, but it turns out that exception&#8217;s stack trace does not contain the full stack. Instead, it contains only frames between the point where the exception <a href=\"https:\/\/ikriv.com\/blog\/?p=596\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[3,4],"tags":[],"class_list":["entry","author-ikriv","post-596","post","type-post","status-publish","format-standard","category-dotnet","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=596"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/596\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}