{"id":73341,"date":"2015-09-15T00:01:00","date_gmt":"2015-09-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/09\/15\/error-handling-two-types-of-errors\/"},"modified":"2019-02-18T09:35:14","modified_gmt":"2019-02-18T16:35:14","slug":"error-handling-two-types-of-errors","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/error-handling-two-types-of-errors\/","title":{"rendered":"Error Handling: Two Types of Errors"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about error handling and two types of errors.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I really don&rsquo;t get it. Everyone tells me I need to use <b>Try<\/b>\/<b>Catch<\/b>\/<b>Finally<\/b> to catch errors, but it simply won&rsquo;t work. I am not sure if there is a bug in Windows PowerShell, but I wish you guys would fix it because it doesn&#039;t work. Can you help me?<\/p>\n<p>&mdash;SH<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello SH,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is becoming fall around here in central Florida. In fact, last night it got down to about 69 degrees Fahrenheit. I mean, it was cold. It is something to look forward to because it has been hot and muggy.<\/p>\n<p>SH, structured error handling in Windows PowerShell does in fact work. The reason that sometimes it may appear to not work is because of the difference in the types of errors. Essentially, there are two types of errors: terminating and non-terminating. A non-terminating error is one that Windows PowerShell can recover from, and it can continue doing things. A terminating error is one that means &quot;it is over, and Windows PowerShell can&rsquo;t do anything else.&quot;<\/p>\n<p>For example, if I attempt to perform a directory listing for a directory that does not exist, Windows PowerShell will simply see that the directory does not exist, throw an error, and continue to the next folder. Most of the times, this is exactly what one wants.<\/p>\n<p>On the other hand, if I attempt to divide by zero, that is a terminating error, and there is nothing else to do.<\/p>\n<p><b>Try<\/b>\/<b>Catch<\/b>\/<b>Finally<\/b> will handle terminating errors. When a terminating error arises (in the <b>Try<\/b> section), it will catch the error (in the <b>Catch<\/b> section). What I do in the <b>Catch<\/b> section is all the error handling stuff. For example, I may want to display the current values of variables and see why my script is attempting to divide by zero, I may want to initialize variables and reset counters (to prevent dividing by zero), or I may simply want to display a message that the error was caught and go on to the next section of the code.<\/p>\n<p>Regardless of what I do in the <b>Try<\/b> or the <b>Catch<\/b> section, the <b>Finally<\/b> section will run. This is great because it is able to, for example, email me the results of my script run, close connections, reboot the computer, or whatever I need to do, or whatever I want to do every time the script runs.<\/p>\n<h2>What do I try?<\/h2>\n<p>So what do I try? Well, in the <b>Try<\/b> section of my script, I have the code that I know, or that I think may create an error if conditions are not right. Therefore, I want to add the error handling around the <b>Try<\/b> section.<\/p>\n<p>If things go wrong, what do I want to happen? That goes into the <b>Catch<\/b> section.<\/p>\n<p>In this script, I try to divide by zero. The code that I want to run goes into the script block. Here is the <b>Try<\/b>:<\/p>\n<p style=\"margin-left:30px\">Try {1\/0}<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<h2>What do I catch?<\/h2>\n<p>I can get really precise with the types of errors that I catch. For example, if I attempt to write to a non-existent file, I can catch the <b>File not found<\/b> error message, create the file, and then in the <b>Finally<\/b> section, write to the file.<\/p>\n<p>But it is also possible to be really generic. For example, all errors derive from System.Exception class. So, if I want to say, &ldquo;Dude, I don&rsquo;t care what happens, I want to catch the error, and then I want to do something,&quot; then I catch [System.Exception].<\/p>\n<p>Of course, what I do when I catch an instance of [System.Exception] goes into the script block. This is shown here:<\/p>\n<p style=\"margin-left:30px\">Catch [System.Exception] {&quot;Caught the exception&quot;}<\/p>\n<h2>What do I do?<\/h2>\n<p>After I have tried something, and I have caught any error that appears, now what? Well, I don&rsquo;t&rsquo; have to do anything, if I don&rsquo;t need to or if I don&rsquo;t want to. But if I do want to do something, such as reset the system, re-initialize variables, or roll back or forward transactions, I can do it in the <b>Finally<\/b> block.<\/p>\n<p>The <b>Finally<\/b> block means that whether things succeeded or failed, I will do it. So, maybe all I do is write to an event log, or send an email confirmation. Here is an example of the <b>Finally<\/b> block:<\/p>\n<p style=\"margin-left:30px\">Finally {$error.Clear() ; &quot;errors cleared&quot;}<\/p>\n<h2>The complete Try\/Catch\/Finally<\/h2>\n<p>If I try to divide by zero, it would generate an error message, such as the one shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\Users\\mredw&gt; 1\/0<\/p>\n<p style=\"margin-left:30px\">Attempted to divide by zero.<\/p>\n<p style=\"margin-left:30px\">At line:1 char:1<\/p>\n<p style=\"margin-left:30px\">+ 1\/0<\/p>\n<p style=\"margin-left:30px\">+ ~~~<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotSpecified: (:) [], RuntimeException<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : RuntimeException<\/p>\n<p>But as it turns out, I put it in the <b>Try<\/b> block. Now, if any error occurs, I want to display a message that says an error was caught. Finally, I want to reset my error count. Here is the complete <b>Try\/Catch\/Finally<\/b> block:<\/p>\n<p style=\"margin-left:30px\">Try {1\/0}<\/p>\n<p style=\"margin-left:30px\">Catch [System.Exception] {&quot;Caught the exception&quot;}<\/p>\n<p style=\"margin-left:30px\">Finally {$error.Clear() ; &quot;errors cleared&quot;}<\/p>\n<p>When I run it, I see two things: the exception was caught and the errors were cleared:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-15-15-01.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-15-15-01.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>SH, that is all there is to using <b>Try\/Catch\/Finally<\/b>. Error Handling Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about error handling and two types of errors. &nbsp;Hey, Scripting Guy! I really don&rsquo;t get it. Everyone tells me I need to use Try\/Catch\/Finally to catch errors, but it simply won&rsquo;t work. I am not sure if there is a bug in Windows PowerShell, but I wish you [&hellip;]<\/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":[1],"tags":[68,3,4,45],"class_list":["post-73341","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-error-handling","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about error handling and two types of errors. &nbsp;Hey, Scripting Guy! I really don&rsquo;t get it. Everyone tells me I need to use Try\/Catch\/Finally to catch errors, but it simply won&rsquo;t work. I am not sure if there is a bug in Windows PowerShell, but I wish you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73341","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=73341"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73341\/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=73341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}