{"id":213,"date":"2019-02-13T23:06:06","date_gmt":"2019-02-13T23:06:06","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/python\/?p=213"},"modified":"2019-03-02T03:56:52","modified_gmt":"2019-03-02T03:56:52","slug":"20-python-exception-handling-1","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/","title":{"rendered":"Python &#8211; Exception Handling &#8211; 1"},"content":{"rendered":"<p>To be a good programmer, you must know how to handle exceptions. Python provide two features to help you handle exceptions. They are:<\/p>\n<ul>\n<li>Exception Handling<\/li>\n<li>Assertions<\/li>\n<\/ul>\n<p>We would cover the following topics<\/p>\n<ol>\n<li><a href=\"#t1\">What are Exceptions<\/a><\/li>\n<li><a href=\"#t2\">Handling Exceptions<\/a><\/li>\n<li><a href=\"#t3\">Some Rules about try Statement<\/a><\/li>\n<li><a href=\"#t4\">The except Clause without Exceptions<\/a><\/li>\n<li><a href=\"#t5\">The Except Clause with Multiple Exceptions<\/a><\/li>\n<li><a href=\"#t6\">The try&#8230;finally Clause<\/a><\/li>\n<li><a href=\"#t7\">Parameters to Exceptions<\/a><\/li>\n<li><a href=\"#t8\">Raising an Exception<\/a><\/li>\n<li><a href=\"#t9\">User-Defined Exception<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. What are Exceptions?<\/strong><\/h4>\n<p>An exception in Python is an event that occurs during the execution of a program that alters the normal flow of the program sequence.<\/p>\n<p>Generally, when a Python program encounters such a situation, it raises an exception. An exception is an object in Python that represents an error.<\/p>\n<p>When an exception is raised, then it must be handled immediately. If not handled, the program would terminate and quit.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Handling Exception<\/strong><\/h4>\n<p>Normally there a codes that have high likelihood of causing exceptions.\u00a0 For such code, you must place them inside a <strong><em>try:<\/em>\u00a0<\/strong>block.<\/p>\n<p>The try block is followed by an <em><strong>except:<\/strong><\/em> statement. Then followed by a block of code that would run if an exception should occur.<\/p>\n<p>The syntax for handling exception is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">try<\/span>: \r\n    code that may cause exception here\r\n    <span style=\"color: #333333;\">...<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">except<\/span> Exception1:\r\n    code to run <span style=\"color: #008800; font-weight: bold;\">if<\/span> Exception1 occurs\r\n    <span style=\"color: #333333;\">...<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">except<\/span> Exception1:\r\n    code to run <span style=\"color: #008800; font-weight: bold;\">if<\/span> Exception2 occurs\r\n    <span style=\"color: #333333;\">...<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">else<\/span>:\r\n    code to run <span style=\"color: #008800; font-weight: bold;\">if<\/span> there <span style=\"color: #000000; font-weight: bold;\">is<\/span> no exception\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3 Some Rules for try Statement<\/strong><\/h4>\n<p>&#8211; A single try block can have more than one except statements. This is necessary where different types of exceptions can be thrown.<\/p>\n<p>&#8211; You can also provide a common except statement, that can handle different types of exceptions<\/p>\n<p>&#8211; After the except block, you can optionally include an else clause. The code in the else block executes whether exception occurs or not<\/p>\n<p>For example, see the code below:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># code to handle IOError<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">try<\/span>:\r\n    myfile <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">open<\/span>(<span style=\"background-color: #fff0f0;\">\"D:\/data\/file.txt\"<\/span>, <span style=\"background-color: #fff0f0;\">\"w+\"<\/span>)\r\n    myfile<span style=\"color: #333333;\">.<\/span>write(<span style=\"background-color: #fff0f0;\">\"Hello World\"<\/span>)\r\n<span style=\"color: #008800; font-weight: bold;\">except<\/span> <span style=\"color: #ff0000; font-weight: bold;\">IOError<\/span>:\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Error occurred: cannot access file\"<\/span>)\r\n<span style=\"color: #008800; font-weight: bold;\">else<\/span>:\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Data has been written to file\"<\/span>)\r\n    myfile<span style=\"color: #333333;\">.<\/span>close()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t4\">4. The except Clause without Exceptions<\/strong><\/p>\n<p>You can also use the except clause without exception. In this case, you except block would handle any type of exception that occurs.<\/p>\n<p>An example is given below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># handle any type of exception<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">try<\/span>:\r\n    code that may cause exception here\r\n    <span style=\"color: #333333;\">...<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">except<\/span>:\r\n    code to run <span style=\"color: #008800; font-weight: bold;\">if<\/span> Exception1 occurs\r\n    <span style=\"color: #333333;\">...<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">else<\/span>:\r\n    code to run <span style=\"color: #008800; font-weight: bold;\">if<\/span> there <span style=\"color: #000000; font-weight: bold;\">is<\/span> no exception\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In case of the code above, you can see that no Exception type is provided. So this would then catch whatever exception that occurs. I would recommend you don&#8217;t handle exceptions this way.<\/p>\n<p>The reason is because it does not provide you a way to identify the actual cause of the problem<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t5\">5. The except Clause with Multiple Exceptions<\/strong><\/p>\n<p>Let&#8217;s now look at a situation where we can include multiple exceptions in the same line. For example, take a look at the code below.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># code to handle multiple exceptions<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">try<\/span>:\r\n   code that may cause exception\r\n   <span style=\"color: #333333;\">...<\/span>\r\n   \r\n<span style=\"color: #008800; font-weight: bold;\">except<\/span>(Exception1[, Exception2[,<span style=\"color: #333333;\">...<\/span>ExceptionN]]]):\r\n   code to execute <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #007020;\">any<\/span> of the listed exceptions occur\r\n   <span style=\"color: #333333;\">...<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">else<\/span>:\r\n   code to execute if exception does not occur\r\n   <span style=\"color: #333333;\">...<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t6\">6. The try-finally Clause<\/strong><\/p>\n<p>Python provides yet another block that you can use. That is the <em><strong>finally:<\/strong> <\/em>block. In the finally block, you place any code that must execute whether exception occurs or not.<\/p>\n<p>The syntax for this is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># the finally block<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">try<\/span>:\r\n   code that may cause exception\r\n   <span style=\"color: #333333;\">...<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">except<\/span>(Exception1[, Exception2[,<span style=\"color: #333333;\">...<\/span>ExceptionN]]]):\r\n   code to execute <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #007020;\">any<\/span> of the listed exceptions occur\r\n   <span style=\"color: #333333;\">...<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">finally<\/span>:\r\n    code to execute whether exception occurs <span style=\"color: #000000; font-weight: bold;\">or<\/span> <span style=\"color: #000000; font-weight: bold;\">not<\/span>\r\n    <span style=\"color: #333333;\">...<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Also note that you cannot use the finally block along with the else block<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t7\">7. Parameters of\u00a0 Exception<\/strong><\/p>\n<p>An exception can accept an argument. The argument specifies value of additional information about the problem that occurred.<\/p>\n<p>The argument depends on the type of exception. For example<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># argument to exception<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">try<\/span>:\r\n   code that may cause exception\r\n   <span style=\"color: #333333;\">...<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">except<\/span> ExceptionType, Argument:\r\n   you can examine the value of the argument\r\n   <span style=\"color: #333333;\">...<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you code handles a single exception, then the argument variable follows the name of the exception, as in the code above. But if you are handling multiple exceptions, then the argument variable would follow the tuple of the exception.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t8\">8. Raising Exceptions<\/strong><\/p>\n<p>You can actually create an exception! You do this using the<strong> raise:<\/strong> statement.\u00a0 The syntax is given below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">raise<\/span> [<span style=\"color: #ff0000; font-weight: bold;\">Exception<\/span> [, args [, traceback]]]\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the code:<\/p>\n<ul>\n<li>Exception is the type of exception.<\/li>\n<li>args is the argument variable<\/li>\n<li>traceback is the traceback object (seldom used)<\/li>\n<\/ul>\n<p>If you raise and exception, then the code following the raise statement is not executed. For example<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">functionName<\/span>( entry ):\r\n   <span style=\"color: #008800; font-weight: bold;\">if<\/span> level <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>:\r\n      <span style=\"color: #008800; font-weight: bold;\">raise<\/span> <span style=\"background-color: #fff0f0;\">\"Incorrect entry\"<\/span>, entry\r\n      <span style=\"color: #888888;\"># The code below to this would not be executed<\/span>\r\n      <span style=\"color: #888888;\"># since we raised an exception<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t9\">9. User-Defined Exceptions<\/strong><\/p>\n<p>In Python, you can create your own exceptions. You do this by deriving from the standard built-in exception classes.<\/p>\n<p>For example, assuming we want to create an exception that derives from RuntimeError.\u00a0 So we create class that derives from RuntimeError. Let&#8217;s call this class NetworkError.<\/p>\n<p>For example, you can see the code below<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># User-defined exceptions<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Networkerror<\/span>(<span style=\"color: #ff0000; font-weight: bold;\">RuntimeError<\/span>):\r\n   <span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">__init__<\/span>(<span style=\"color: #007020;\">self<\/span>, arg):\r\n      <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>args <span style=\"color: #333333;\">=<\/span> arg\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So, once we have this class, then we can use it. You use by raising an exception with it. This is shown in the code below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Raising a User-defined error<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">try<\/span>:\r\n   <span style=\"color: #008800; font-weight: bold;\">raise<\/span> Networkerror(<span style=\"background-color: #fff0f0;\">\"Invalid address\"<\/span>)\r\n<span style=\"color: #008800; font-weight: bold;\">except<\/span> Networkerror,e:\r\n   <span style=\"color: #007020;\">print<\/span> e<span style=\"color: #333333;\">.<\/span>args\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To be a good programmer, you must know how to handle exceptions. Python provide two features to help you handle exceptions. They are: Exception Handling &hellip; <\/p>\n","protected":false},"author":395,"featured_media":214,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[19],"class_list":["post-213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-python-exception-handling"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python - Exception Handling - 1 - Python Tutorials<\/title>\n<meta name=\"description\" content=\"In this lesson, you learn about errors and exceptions in Python and how to handle them. How to use try...except statement is covered.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - Exception Handling - 1 - Python Tutorials\" \/>\n<meta property=\"og:description\" content=\"In this lesson, you learn about errors and exceptions in Python and how to handle them. How to use try...except statement is covered.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-13T23:06:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-02T03:56:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Exception-Handling-in-Python1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1005\" \/>\n\t<meta property=\"og:image:height\" content=\"544\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Python &#8211; Exception Handling &#8211; 1\",\"datePublished\":\"2019-02-13T23:06:06+00:00\",\"dateModified\":\"2019-03-02T03:56:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/\"},\"wordCount\":680,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Exception-Handling-in-Python1.jpg\",\"keywords\":[\"Python Exception Handling\"],\"articleSection\":[\"Python Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/\",\"name\":\"Python - Exception Handling - 1 - Python Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Exception-Handling-in-Python1.jpg\",\"datePublished\":\"2019-02-13T23:06:06+00:00\",\"dateModified\":\"2019-03-02T03:56:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"In this lesson, you learn about errors and exceptions in Python and how to handle them. How to use try...except statement is covered.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Exception-Handling-in-Python1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Exception-Handling-in-Python1.jpg\",\"width\":1005,\"height\":544,\"caption\":\"Exception Handling in Python1\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/20-python-exception-handling-1\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python &#8211; Exception Handling &#8211; 1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\",\"name\":\"Python Tutorials\",\"description\":\"Python Tutorial for Programming and Data Science\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python - Exception Handling - 1 - Python Tutorials","description":"In this lesson, you learn about errors and exceptions in Python and how to handle them. How to use try...except statement is covered.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/","og_locale":"en_US","og_type":"article","og_title":"Python - Exception Handling - 1 - Python Tutorials","og_description":"In this lesson, you learn about errors and exceptions in Python and how to handle them. How to use try...except statement is covered.","og_url":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/","og_site_name":"Python Tutorials","article_published_time":"2019-02-13T23:06:06+00:00","article_modified_time":"2019-03-02T03:56:52+00:00","og_image":[{"width":1005,"height":544,"url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Exception-Handling-in-Python1.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Python &#8211; Exception Handling &#8211; 1","datePublished":"2019-02-13T23:06:06+00:00","dateModified":"2019-03-02T03:56:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/"},"wordCount":680,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Exception-Handling-in-Python1.jpg","keywords":["Python Exception Handling"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/","url":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/","name":"Python - Exception Handling - 1 - Python Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Exception-Handling-in-Python1.jpg","datePublished":"2019-02-13T23:06:06+00:00","dateModified":"2019-03-02T03:56:52+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"In this lesson, you learn about errors and exceptions in Python and how to handle them. How to use try...except statement is covered.","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Exception-Handling-in-Python1.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Exception-Handling-in-Python1.jpg","width":1005,"height":544,"caption":"Exception Handling in Python1"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/python\/20-python-exception-handling-1\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/python\/"},{"@type":"ListItem","position":2,"name":"Python &#8211; Exception Handling &#8211; 1"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/python\/#website","url":"https:\/\/www.kindsonthegenius.com\/python\/","name":"Python Tutorials","description":"Python Tutorial for Programming and Data Science","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/python\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/213\/revisions\/217"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media\/214"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}