{"id":3893,"date":"2008-10-09T11:00:00","date_gmt":"2008-10-09T11:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2008\/10\/09\/vb-catch-when-why-so-special\/"},"modified":"2024-07-05T14:06:47","modified_gmt":"2024-07-05T21:06:47","slug":"vb-catch-when-why-so-special","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/vb-catch-when-why-so-special\/","title":{"rendered":"VB Catch &#8230; When: Why so special?"},"content":{"rendered":"<p>The VB Catch syntax has a unique feature: When.&nbsp; It allows users to filter expressions based on something other than their type.&nbsp; Any arbitrary code can enter a When block to decide whether or not to handle an Exception.<\/p>\n<pre class=\"code\">    <span>Sub <\/span>Sub1()\n        <span>Try\n            <\/span>DoSomeAction()\n        <span>Catch <\/span>ex <span>As <\/span>Exception <span>When <\/span>Filter(ex)\n            <span>Stop\n        End Try\n    End Sub\n<\/span><\/pre>\n<p><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a><\/p>\n<p>Newsgroups often ask, &#8220;Why&#8217;s this so special? I could effectively get the same behavior out of C# by doing the following:&#8221;<\/p>\n<pre class=\"code\"><span>static void <\/span>Sub1()\n{\n    <span>try\n    <\/span>{\n        DoSomeAction();\n    }\n    <span>catch <\/span>(<span>Exception <\/span>ex)\n    {\n        <span>if <\/span>(Filter(ex))\n        {\n            <span>throw<\/span>;\n        }\n        HandleException();\n    }\n}<\/pre>\n<p><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a><\/p>\n<p>This is true to an extent.&nbsp; In both cases the code is handling an exception and making a decision, via calling Filter, as to whether or not to handle the exception.&nbsp; The subtle difference is when the Filter method is called.&nbsp; <\/p>\n<p>In VB the When statement is actually implemented as an IL exception filter.&nbsp; When an exception is thrown, exception filters are processed before the stack is unwound.&nbsp; This means that if the Filter method created an error report that included the current stack trace, it would show the frame in which the exception occurred.&nbsp; <\/p>\n<p>For example, in the code above if DoSomeAction() threw and the stack was examined in the Filter expression, the following stack would show up. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/7\/2019\/02\/image_2.png\"><img decoding=\"async\" height=\"304\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/vbteam\/wp-content\/uploads\/sites\/7\/2008\/10\/image_thumb.png\" width=\"504\" border=\"0\"><\/a> <\/p>\n<p>Notice how the DoSomeAction method is clearly visible?&nbsp; This is incredibly powerful for features like error reporting and investigation.&nbsp; It also allows you to set powerful breakpoints where the exact state of the error can be examined and not just the post mortem.&nbsp; <\/p>\n<p>Alternatively, code executed in the C# block will occur after the stack is unwound.&nbsp; As long as you&#8217;re not in optimized code you can usually use the stack trace properties to get the source of the exception, but you won&#8217;t be able to examine the live state of the error. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/7\/2019\/02\/image_4.png\"><img decoding=\"async\" height=\"304\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/vbteam\/wp-content\/uploads\/sites\/7\/2008\/10\/image_thumb_1.png\" width=\"501\" border=\"0\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The VB Catch syntax has a unique feature: When.&nbsp; It allows users to filter expressions based on something other than their type.&nbsp; Any arbitrary code can enter a When block to decide whether or not to handle an Exception. Sub Sub1() Try DoSomeAction() Catch ex As Exception When Filter(ex) Stop End Try End Sub Newsgroups [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[57,78,165,166,167],"class_list":["post-3893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-did-you-know","tag-jared-parsons","tag-vb2005","tag-vb2008","tag-vb2010"],"acf":[],"blog_post_summary":"<p>The VB Catch syntax has a unique feature: When.&nbsp; It allows users to filter expressions based on something other than their type.&nbsp; Any arbitrary code can enter a When block to decide whether or not to handle an Exception. Sub Sub1() Try DoSomeAction() Catch ex As Exception When Filter(ex) Stop End Try End Sub Newsgroups [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/3893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=3893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/3893\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=3893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=3893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=3893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}