{"id":37326,"date":"2016-08-31T12:55:42","date_gmt":"2016-08-31T17:55:42","guid":{"rendered":"http:\/\/luispalominotblog.wordpress.com\/?p=716"},"modified":"2016-08-31T12:55:42","modified_gmt":"2016-08-31T17:55:42","slug":"you-are-the-only-exception","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/you-are-the-only-exception\/","title":{"rendered":"You are the only Exception"},"content":{"rendered":"<p class=\"syndicated-attribution\"><em> --Originally published at <a href=\"https:\/\/luispalominotblog.wordpress.com\/2016\/08\/31\/you-are-the-only-exception\/\"><a href=\"https:\/\/luispalominotblog.wordpress.com\">Programming<\/a><\/a><\/em><br \/><\/p>\n<h2>Introduction to Exceptions<\/h2>\n<p>It&#8217;s been a long journey for some of you. Whether you&#8217;ve spent 10+ hours coding or just a couple, I&#8217;m pretty sure you are familiar with the error messages that the console displays when your code is wrong. So far you don&#8217;t care if the user enters a number when they are supposed to enter a string, but that&#8217;s from the past. It&#8217;s now time for us to take the next step and start handling exceptions. By now you should know that there\u00a0are at least two important noticeable\u00a0kinds of errors: syntax errors and exceptions.<\/p>\n<h2>Syntax Errors<\/h2>\n<p>Probably one of the most common kind of errors for the people that are still in the process of learning Python. The console will print a Syntax Error message showing you with an arrow ^ the earliest point in the line where the error was detected, followed by the number of the line so you\u00a0know where to look.<br \/>\n<img class=\" size-full wp-image-740 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/116.png?w=775\" alt=\"1.png\"   \/><\/p>\n<p><img class=\" size-full wp-image-742 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/215.png?w=775\" alt=\"2.png\"   \/><\/p>\n<h2>Exceptions<\/h2>\n<p>A statement or an expression can be syntactically correct and yet,\u00a0an error may occur when the program is executed. This is caused because of an exception, which is defined as an error occurred during the execution of a program. Exceptions come in different types, some types\u00a0for the exceptions are:<\/p>\n<ul>\n<li>ZeroDivisionError (Divide a number by 0)<\/li>\n<li>TypeError(When an operation or function is applied to an object of inappropriate type)<\/li>\n<li>ImportError(When an import statement fails to find the module)<\/li>\n<li>IndexError(When a sequence substring is out of range)<\/li>\n<li>KeyError(When a Dictionary key is not found in the set of existing keys)<\/li>\n<li>ValueError()<\/li>\n<\/ul>\n<p>Check out the documentation for the Python 3.5 Built-in Exceptions\u00a0<a href=\"https:\/\/docs.python.org\/3\/library\/exceptions.html#bltin-exceptions\" >here<\/a><\/p>\n<h2>Handling Exceptions<\/h2>\n<p>Now that we know some types of exceptions, it&#8217;s time for us to learn how to handle\u00a0them. Let&#8217;s take a look at the following example where the program asks <div class=\"post-limited-image\"><img class=\" size-full wp-image-783 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/312.png?w=775\" alt=\"3.png\"   \/><\/div>\n<div class=\"post-limited-image\"><img class=\"alignnone size-full wp-image-830\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/56.png?w=775\" alt=\"5.png\"   \/><\/div>\n<div class=\"post-limited-image\"><img class=\" size-full wp-image-827 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/63.png?w=775\" alt=\"6.png\"   \/><\/div>\n<div class=\"post-limited-image\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/luispalominotblog.wordpress.com\/716\/\" \/><\/div>\n<div class=\"post-limited-image\"><img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=luispalominotblog.wordpress.com&#038;blog=115253528&%23038;post=716&%23038;subd=luispalominotblog&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/><\/div><!--more--> user for a number. Considering that the user enters a string &#8220;a&#8221; instead of a number the program will detect an error within the try clause, the rest of the clause will be skipped and the\u00a0clause after the <em>ValueError<\/em> exception will be ran.<\/p>\n<p><img class=\" size-full wp-image-783 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/312.png?w=775\" alt=\"3.png\"   \/><\/p>\n<h4>How it works<\/h4>\n<p>First of all, you need to know where to put your exception handler. For example, if you know that the user will mess something up, you are going to want to take some precautions.<\/p>\n<ol>\n<li>The code inside the try clause is executed.<\/li>\n<li>If\u00a0no exceptions were found, the program will ignore\u00a0the except clause and continue as if nothing&#8217;d happened.<\/li>\n<li>If the program runs into an exception, the rest of the try clause will be skipped and the except clause will be executed if and only if the exception occurred matches the exception named after the except keyword.<\/li>\n<li>If the exceptions do not match, then the exception will become an unhandled exception and an error message will be displayed in the console.<\/li>\n<\/ol>\n<p>A try statement may have more than one except clause, to identify handlers for different exceptions. An except clause may name multiple exceptions as a tuple. For example:<\/p>\n<p style=\"text-align:center;\"><em>except (ValueError, ZeroDivisionError,KeyError):<\/em><\/p>\n<h4>Else and Finally clauses<\/h4>\n<p>The else clause is optional. It is really useful when the try clause doesn&#8217;t raise any exceptions. The finally clause is also optional and it&#8217;s going to be executed no matter what. Basically the finally clause will always be executed.<\/p>\n<h4>Example<\/h4>\n<h4><img class=\"alignnone size-full wp-image-830\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/56.png?w=775\" alt=\"5.png\"   \/><\/h4>\n<p><img class=\" size-full wp-image-827 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/63.png?w=775\" alt=\"6.png\"   \/><\/p><br \/>  <a rel=\"nofollow\" href=\"http:\/\/feeds.wordpress.com\/1.0\/gocomments\/luispalominotblog.wordpress.com\/716\/\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/luispalominotblog.wordpress.com\/716\/\" \/><\/a> <img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=luispalominotblog.wordpress.com&#038;blog=115253528&%23038;post=716&%23038;subd=luispalominotblog&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/>","protected":false},"excerpt":{"rendered":"<p>&#8211;Originally published at Programming Introduction to Exceptions It&#8217;s been a long journey for some of you. Whether you&#8217;ve spent 10+ hours coding or just a couple, I&#8217;m pretty sure you are familiar with the error messages that the console displays when your code is wrong. So far you don&#8217;t care if the user enters a &hellip; <a href=\"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/you-are-the-only-exception\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">You are the only Exception<\/span><\/a><\/p>\n","protected":false},"author":14,"featured_media":740,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[163,24,33,28],"class_list":["post-37326","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs","category-syndicated","tag-exceptions","tag-python","tag-tc101","tag-uncategorized"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/37326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/comments?post=37326"}],"version-history":[{"count":17,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/37326\/revisions"}],"predecessor-version":[{"id":42899,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/37326\/revisions\/42899"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media?parent=37326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/categories?post=37326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/tags?post=37326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}