{"id":83035,"date":"2018-05-31T11:00:52","date_gmt":"2018-05-31T18:00:52","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=83035"},"modified":"2019-02-18T09:09:57","modified_gmt":"2019-02-18T16:09:57","slug":"powertip-ensure-that-errors-in-powershell-are-caught","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-ensure-that-errors-in-powershell-are-caught\/","title":{"rendered":"PowerTip: Ensure that errors in PowerShell are caught"},"content":{"rendered":"<p><strong>Summary<\/strong>: Here\u2019s how to make sure your errors get caught with <strong>Try Catch Finally<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\"><img decoding=\"async\" width=\"34\" height=\"34\" class=\"alignnone size-full wp-image-82735\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" \/><\/a> \u00a0\u00a0 Hey, Scripting Guy! I\u2019ve been trying to use the <strong>Try Catch Finally<\/strong>, but some of my errors aren\u2019t getting caught. What could be the cause?<\/p>\n<p style=\"padding-left: 60px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\"><img decoding=\"async\" width=\"34\" height=\"34\" class=\"alignnone size-full wp-image-82745\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" \/><\/a> \u00a0\u00a0 For <strong>Try Catch Finally<\/strong>, you need to make sure the error defaults to a \u201cStop\u201d action for the cmdlet in question. Here\u2019s a quick example:<\/p>\n<p style=\"padding-left: 60px\"><code>try<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>{<\/code><\/p>\n<p style=\"padding-left: 120px\"><code>Get-Childitem c:\\Foo -ErrorAction stop<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>}<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>catch [System.Management.Automation.ItemNotFoundException]<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>{<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>'oops, I guess that folder was not there'<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>}<\/code><\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 60px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/dr_scripto.png\"><img decoding=\"async\" width=\"96\" height=\"145\" class=\"alignnone size-full wp-image-82775\" alt=\"Drawing of Dr. Scripto\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/dr_scripto.png\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here\u2019s how to make sure your errors get caught with <strong>Try Catch Finally<\/strong>.<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[639],"tags":[356,3,154,45],"class_list":["post-83035","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powertip","tag-powertip","tag-scripting-guy","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Here\u2019s how to make sure your errors get caught with <strong>Try Catch Finally<\/strong>.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/83035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=83035"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/83035\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=83035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=83035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=83035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}