{"id":17911,"date":"2017-07-19T16:15:14","date_gmt":"2017-07-19T13:15:14","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=17911"},"modified":"2018-01-09T09:35:10","modified_gmt":"2018-01-09T07:35:10","slug":"python-exception-handling","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/","title":{"rendered":"Python Try and Except Example"},"content":{"rendered":"<p>Today we will talk about one of the most important things in Python &#8211; exception handling. Firstly, let&#8217;s start with the definition of exceptions. They are errors detected while the program is being executed. So for example, if you write the program and you think it&#8217;s alright and try running it, it may give you some errors.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n[ulp id=&#8217;R7QVpFMZmjosABLC&#8217;]<\/p>\n<h2>1. Basic exceptions<\/h2>\n<p>Let&#8217;s start with some basic exceptions.<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline\"><em>Python Shell<\/em><\/span><\/p>\n<pre class=\"brush:bash\">\r\n&gt;&gt;&gt; 1\/0\r\nTraceback (most recent call last):\r\n  File \"\", line 1, in \r\nZeroDivisionError: division by zero\r\n<\/pre>\n<p>So this program gives us the first error, because we can&#8217;t divide the number by zero. Let&#8217;s try another one.<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline\"><em>Python Shell<\/em><\/span><\/p>\n<pre class=\"brush:bash\">\r\n&gt;&gt;&gt; \"string\" + 5\r\nTraceback (most recent call last):\r\n  File \"\", line 1, in \r\nTypeError: must be str, not int\r\n<\/pre>\n<p>So we can&#8217;t concatenate the string with the integer number. When the program &#8220;crashes&#8221;, it means it finished execution with the result of error. <\/p>\n<h2>2. Exception handling<\/h2>\n<p>Now we are familiar with some of the errors that we may encounter while writing a program. Let&#8217;s try to handle those errors. We will create the basic program the takes two numbers and returns the result of its division.<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nfirst = input()\r\nsecond = input()\r\nresult = first\/second\r\nprint(result)\r\n<\/pre>\n<p>What happens when we execute the program? Try it for yourself! You will get a <code>TypeError<\/code>. The reason is simple: when we use <code>input()<\/code> method, we put the data in variables as <code>string<\/code>, not as an <code>integer<\/code> which we need. So how can we solve it? There are actually two ways to solve it: we may convert them to <code>int<\/code> while asking a user for input, or we can convert them while dividing. I will show both ways.<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nfirst = int(input())\r\nsecond = int(input())\r\nresult = first\/second\r\nprint(result)\r\n<\/pre>\n<p>This one is preferrable if you know that you will be working with integers only. Another way is following:<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nfirst = input()\r\nsecond = input()\r\nresult = int(first)\/int(second)\r\nprint(result)\r\n<\/pre>\n<p>This one is preferrable if you know that the user may input some strings which you want to operate (but why would you need division in the first place then?). Anyway, I prefer the first method more.<br \/>\nSo what happens if <code>second<\/code> is zero? We get <code>ZeroDivisionError: division by zero<\/code> error. Let&#8217;s handle it using <code>try<\/code> and <code>except<\/code>.<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nfirst = input()\r\nsecond = input()\r\ntry:\r\n   result = int(first)\/int(second)\r\n   print(result)\r\nexcept Exception as e:\r\n   print(e)\r\n<\/pre>\n<p>What we do here is that we actually print the type of error which occurs while executing. So if we try to divide by zero, we get <code>division by zero<\/code>. We didn&#8217;t get any red lines, the program didn&#8217;t crash, so we handled the error successfully!<br \/>\nYou may ask, &#8220;Hey, we only handle one error! What about the rest?&#8221;. I am glad that you asked (if you didn&#8217;t, don&#8217;t worry. I am glad that you are reading this article now anyway). So what happens if we enter strings, not integers? The program crashes, right? Let&#8217; handle it.<\/p>\n<p>Firstly, let&#8217;s think what we need to do. We ask a user for input and convert it to integer right away. Then, depending on the type of variables we do the following: if both are integer, we do calculate the division and handle <code>DivisionByZero<\/code> exception as well, if at least one of the variables isn&#8217;t an integer, we show the user a warning message, and the program finishes. So what I want to do is to actually build a function that will do handling division error and division itself. Let&#8217;s call it <code>division<\/code>. Then let&#8217;s call it only if both variables are integers.<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\n#division function\r\ndef division():\r\n   try:\r\n      result = first \/ second\r\n      print(result)\r\n   except Exception as e:\r\n      print(e)\r\n#it's important to have division function before the code below\r\ntry:\r\n   first = int(input())\r\n   second = int(input())\r\n   division()\r\nexcept Exception as e:\r\n   print(\"You can't put anything but integer numbers\")\r\n<\/pre>\n<p>That&#8217;s about it. Let&#8217;s advance this program into one more step, shall we? What if we want to run the program until it actually gives us the result. What I mean is that right now the program stops while we put something but integer number. We want from a user to eventually put appropriate numbers, so the program will give us some result. The logic is quite similar (even though there are easier ways to do this, but this one seems pretty easy to follow). Let&#8217;s create the function <code>calculation()<\/code> which will be the main one. We will still have the function <code>division()<\/code>.<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline\"><em>calculation.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\ndef division(first, second):\r\n    try:\r\n        result = first \/ second\r\n        print(result)\r\n    except Exception as e:\r\n        print(e)\r\n\r\ndef calculation():\r\n    try:\r\n        first = int(input())\r\n        second = int(input())\r\n        division(first, second)\r\n    except Exception as e:\r\n        print(\"You can't put anything but integer numbers\")\r\n        calculation()\r\ncalculation()\r\n<\/pre>\n<p>So now, try running it, and you will be able to get the result eventually.<\/p>\n<h2>3. Raising exceptions<\/h2>\n<p> Sometimes we would need to raise exceptions to debug our code. Usually, when you create your own exception, the best way is to create the class. Let&#8217;s do it:<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline\"><em>raise.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nclass Accident(Exception):\r\n    def __init__(self, message):\r\n        self.message = message\r\n    def print_exception(self):\r\n        print(\"Our custom exception is...\", self.message)\r\n\r\ntry:\r\n    raise Accident(\"here\")\r\nexcept Accident as a:\r\n    a.print_exception()\r\n<\/pre>\n<p>So this is how we raise the exception: we create the class where we initialize the statement which will be printed, once the exception is raised. So our code raises an exception.<\/p>\n<h2>4. Finally statement <\/h2>\n<p>Finally statement is used to do the clean-up. For example, the code with <code>finally<\/code> statement will run no matter what.<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline\"><em>finally.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\ndictionary = {\"a\":0, \"b\":1, \"c\":2}\r\ntry:\r\n   value = dictionary[input()]\r\nexcept KeyError:\r\n    print(\"An error has occured!\")\r\nelse:\r\n    print(\"Everything's fine\")\r\nfinally:\r\n    print(\"It's finally over!\")\r\n<\/pre>\n<p>So a user puts a <code>Key<\/code> that will look for a matching key in the dictionary. If there is no match, it prints <code>\"An error has occured!\"<\/code>. If there is a match, it prints <code>\"Everything's fine\"<\/code>. It prints <code>\"It's finally over!\"<\/code> no matter what.<\/p>\n<h2>5. Summary<\/h2>\n<p>So handling exceptions is a very important tool that is executed while debugging your program. You should definitely learn how to handle exceptions. Let&#8217;s sum up what we need to know:<\/p>\n<ul>\n<li><code>try<\/code> is used to try to execute the code. If there are no errors, the program will finish successfully.<\/li>\n<li><code>except<\/code> is used for finding exceptions. You can handle multiple exceptions just writing another <code>except<\/code> block of code. There are many built-in exceptions that you can find in the official documentation.<\/li>\n<li><code>raise<\/code> is used to raise your own exceptions. Once again, it&#8217;s not commonly used in production but can be quite handy in testing and debugging. <\/li>\n<li><code>finally<\/code> is used to test whether the program iterates correctly. For example, you may have a lot of <code>if-else<\/code> statments and you are not sure if it iterates the right block. You can put <code>finally<\/code> there to understand if the program runs correctly or not. <\/li>\n<\/ul>\n<h2>6. Homework<\/h2>\n<p>I hope error handling seems more logical for you now (if you were struggling with it). In fact, the best way to learn about errors and exceptions is to actually write code. It plays vital role in DRY principle (as known as <b>D<\/b>on\u2019t <b>R<\/b>epeat <b>Y<\/b>ourself).<br \/>\nSo here is some homework for you:<\/p>\n<p><i>Try to create a calculator in Python which will handle errors like putting strings, putting non-ASCII characters or very large numbers. Try to think about the time needed to calculate all inputs and make it as fast as possible. Try to avoid recursions (even though they look cool, they may slow your program down).<\/i><\/p>\n<h2>7. Download the Source Code<\/h2>\n<p>You can find all materials explained in this article below<\/p>\n<div class=\"download\"><strong>Downoload<\/strong><br \/> You can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2017\/07\/try-catch.zip\"><strong>try-catch.zip<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today we will talk about one of the most important things in Python &#8211; exception handling. Firstly, let&#8217;s start with the definition of exceptions. They are errors detected while the program is being executed. So for example, if you write the program and you think it&#8217;s alright and try running it, it may give you &hellip;<\/p>\n","protected":false},"author":657,"featured_media":1651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[476,478,479,290,477],"class_list":["post-17911","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-error","tag-except","tag-finally","tag-python","tag-try"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Try and Except Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Today we will talk about one of the most important things in Python - exception handling. Firstly, let&#039;s start with the definition of exceptions. They are\" \/>\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.webcodegeeks.com\/python\/python-exception-handling\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Try and Except Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Today we will talk about one of the most important things in Python - exception handling. Firstly, let&#039;s start with the definition of exceptions. They are\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-19T13:15:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T07:35:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Aleksandr Krasnov\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aleksandr Krasnov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/\"},\"author\":{\"name\":\"Aleksandr Krasnov\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373\"},\"headline\":\"Python Try and Except Example\",\"datePublished\":\"2017-07-19T13:15:14+00:00\",\"dateModified\":\"2018-01-09T07:35:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/\"},\"wordCount\":1067,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"keywords\":[\"error\",\"except\",\"finally\",\"python\",\"try\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/\",\"name\":\"Python Try and Except Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"datePublished\":\"2017-07-19T13:15:14+00:00\",\"dateModified\":\"2018-01-09T07:35:10+00:00\",\"description\":\"Today we will talk about one of the most important things in Python - exception handling. Firstly, let's start with the definition of exceptions. They are\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Try and Except Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373\",\"name\":\"Aleksandr Krasnov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g\",\"caption\":\"Aleksandr Krasnov\"},\"description\":\"Aleksandr is passionate about teaching programming. His main interests are Neural Networks, Python and Web development. Hobbies are game development and translating. For the past year, he has been involved in different international projects as SEO and IT architect.\",\"url\":\"https:\/\/www.webcodegeeks.com\/author\/aleksandr-krasnov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Try and Except Example - Web Code Geeks - 2026","description":"Today we will talk about one of the most important things in Python - exception handling. Firstly, let's start with the definition of exceptions. They are","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.webcodegeeks.com\/python\/python-exception-handling\/","og_locale":"en_US","og_type":"article","og_title":"Python Try and Except Example - Web Code Geeks - 2026","og_description":"Today we will talk about one of the most important things in Python - exception handling. Firstly, let's start with the definition of exceptions. They are","og_url":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2017-07-19T13:15:14+00:00","article_modified_time":"2018-01-09T07:35:10+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","type":"image\/jpeg"}],"author":"Aleksandr Krasnov","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Aleksandr Krasnov","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/"},"author":{"name":"Aleksandr Krasnov","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373"},"headline":"Python Try and Except Example","datePublished":"2017-07-19T13:15:14+00:00","dateModified":"2018-01-09T07:35:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/"},"wordCount":1067,"commentCount":1,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","keywords":["error","except","finally","python","try"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/","url":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/","name":"Python Try and Except Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","datePublished":"2017-07-19T13:15:14+00:00","dateModified":"2018-01-09T07:35:10+00:00","description":"Today we will talk about one of the most important things in Python - exception handling. Firstly, let's start with the definition of exceptions. They are","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/python\/python-exception-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/www.webcodegeeks.com\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Try and Except Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373","name":"Aleksandr Krasnov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g","caption":"Aleksandr Krasnov"},"description":"Aleksandr is passionate about teaching programming. His main interests are Neural Networks, Python and Web development. Hobbies are game development and translating. For the past year, he has been involved in different international projects as SEO and IT architect.","url":"https:\/\/www.webcodegeeks.com\/author\/aleksandr-krasnov\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/17911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/657"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=17911"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/17911\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/1651"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=17911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=17911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=17911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}