{"id":3851,"date":"2009-06-17T00:05:19","date_gmt":"2009-06-17T00:05:19","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/06\/17\/traps-vs-trycatch\/"},"modified":"2019-02-18T13:12:34","modified_gmt":"2019-02-18T20:12:34","slug":"traps-vs-trycatch","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/traps-vs-trycatch\/","title":{"rendered":"Traps vs Try\/Catch"},"content":{"rendered":"<p>Someone as asking about Traps vs Try\/Catch and Jason Shirk (PowerShell developer) gave a nice concise and precise answer that I thought I would share with you:<\/p>\n<ul>\n<li>Trap:<\/li>\n<ul>\n<li>Designed for admins<\/li>\n<li>V1 and V2<\/li>\n<li>Introduces a new scope<\/li>\n<li>Is &#8220;global&#8221;, meaning it applies to all code in the same scope, before or after.<\/li>\n<li>Does not support rethrow (an empty throw statement throws a special RuntimeException with the message &#8220;ScriptHalted&#8221;)<\/li>\n<\/ul>\n<li>Try\/Catch<\/li>\n<ul>\n<li>Designed for developers<\/li>\n<li>V2 only<\/li>\n<li>Does not introduce a new scope<\/li>\n<li>Guarded code is in the try statement block, not the entire scope containing the try statement<\/li>\n<li>Supports finally<\/li>\n<li>Supports rethrowing exceptions<\/li>\n<\/ul>\n<\/ul>\n<p>Enjoy! <\/p>\n<p>Jeffrey Snover [MSFT]   <br \/>Distinguished Engineer    <br \/>Visit the Windows PowerShell Team blog at:&#160;&#160;&#160; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a>    <br \/>Visit the Windows PowerShell ScriptCenter at:&#160; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Someone as asking about Traps vs Try\/Catch and Jason Shirk (PowerShell developer) gave a nice concise and precise answer that I thought I would share with you: Trap: Designed for admins V1 and V2 Introduces a new scope Is &#8220;global&#8221;, meaning it applies to all code in the same scope, before or after. Does not [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3851","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Someone as asking about Traps vs Try\/Catch and Jason Shirk (PowerShell developer) gave a nice concise and precise answer that I thought I would share with you: Trap: Designed for admins V1 and V2 Introduces a new scope Is &#8220;global&#8221;, meaning it applies to all code in the same scope, before or after. Does not [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=3851"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3851\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=3851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}