{"id":79304,"date":"2020-07-05T09:00:49","date_gmt":"2020-07-05T03:30:49","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=79304"},"modified":"2020-07-05T09:00:49","modified_gmt":"2020-07-05T03:30:49","slug":"errors-and-exceptions-in-python","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/","title":{"rendered":"Errors and Exceptions in Python"},"content":{"rendered":"<p>No matter how good you make your <strong>code<\/strong>, things can go wrong.<\/p>\n<p>When we <strong>plan our program<\/strong>, we always work for <strong>situations<\/strong> we normally expect. And our program <strong>works<\/strong> quite well in those <strong>situations<\/strong>. But we often <strong>ignore<\/strong> the <strong>possibility<\/strong> of <strong>unexpected situations<\/strong> which can <strong>mess<\/strong> with the<strong> normal execution<\/strong> of our <strong>program<\/strong>.<\/p>\n<p>Let\u2019s learn about such <strong>situations<\/strong> and what exactly happens when our program encounters <strong>Python error<\/strong> and <strong>exception<\/strong>.<\/p>\n<h3>Python Error<\/h3>\n<p>You might have come across <strong>syntax<\/strong> <strong>errors<\/strong> millions of times in your <strong>initial stages<\/strong> of learning <strong>Python<\/strong>. Everybody does. And even if your <strong>syntax<\/strong> is <strong>correct<\/strong>, you might <strong>encounter errors<\/strong> while <strong>execution<\/strong> of your <strong>program<\/strong>.<\/p>\n<p><strong>Errors<\/strong> are <strong>unusual<\/strong> and <strong>unexpected situations<\/strong> that affect the<strong> normal flow<\/strong> of program <strong>execution<\/strong>. We make <strong>errors<\/strong> all the time.<\/p>\n<p>Some of these errors are<strong> \u201ccompile-time\u201d<\/strong> <strong>errors<\/strong>. These errors occur before the program execution begins, i.e., <strong>at the<\/strong> <strong>time of compilation<\/strong>.<\/p>\n<p>While the other errors are <strong>\u201crun-time\u201d<\/strong> errors and occur after the <strong>code<\/strong> is <strong>successfully<\/strong> <strong>compiled<\/strong> and is <strong>running<\/strong>.<\/p>\n<p>The<strong> two distinguishable<\/strong> types of errors in Python are:<\/p>\n<ul>\n<li><strong>Syntax errors &#8211;<\/strong> Compile-time error<\/li>\n<li><strong>Exceptions &#8211;<\/strong> Run-time errors<\/li>\n<\/ul>\n<p>Let\u2019s have a look at Syntax errors before talking about the<strong> \u2018elephant in the room\u2019<\/strong>&#8211; Exceptions.<\/p>\n<h3>Python Syntax Error<\/h3>\n<p>A <strong>syntax error<\/strong> occurs when you don\u2019t follow proper <strong>rules<\/strong> and <strong>structure<\/strong> of the <strong>language<\/strong>. A very common mistake that we make while getting <strong>accustomed<\/strong> to Python\u2019s syntax is <strong>not using correct indentation<\/strong>. This results in a <strong>syntax error<\/strong>.<\/p>\n<p>Another example would be<strong> not putting a colon<\/strong> after a <strong>function header<\/strong>, or not <strong>enclosing<\/strong> a <strong>string within quotes<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; l = [1, 2, 3]\n\n&gt;&gt;&gt; for i in l\nSyntaxError: invalid syntax\n&gt;&gt;&gt; \n\n<\/pre>\n<p>Recognize the <strong>error<\/strong> here?<\/p>\n<p>We forgot to put a colon after <strong>\u201cfor i in l\u201d<\/strong>. And as we press <strong>Enter<\/strong> to start the <strong>for-loop\u2019s<\/strong> block, we get a <strong>SyntaxError<\/strong> message.<\/p>\n<p>There are so many places in your <strong>code<\/strong> where you can possibly <strong>go wrong<\/strong>. And as soon as the <strong>compiler recognizes<\/strong> your <strong>mistake<\/strong>, it <strong>pops up<\/strong> a <strong>syntax error message<\/strong>.<\/p>\n<p>Your <strong>program<\/strong> will <strong>not start executing<\/strong> until you <strong>correct <\/strong>all the <strong>syntax errors<\/strong> in your <strong>code<\/strong>.<\/p>\n<h3>Python Exceptions<\/h3>\n<p>Even if you get the <strong>syntax<\/strong> <strong>right<\/strong>, your program might get <strong>stuck somewhere<\/strong> during <strong>execution<\/strong>. This is due to the <strong>errors<\/strong> in your <strong>code<\/strong> which occur at <strong>run-time<\/strong>. There is no way for your <strong>compiler<\/strong> to <strong>recognize<\/strong> such <strong>errors<\/strong>.<\/p>\n<p><strong>Exceptions<\/strong> in python are the <strong>errors<\/strong> that occur during <strong>execution<\/strong>. Well simply put, <strong>exceptions occur<\/strong> when your <strong>program<\/strong> <strong>tries<\/strong> to <strong>perform<\/strong> an <strong>operation<\/strong> it wasn\u2019t supposed to. This is when your <strong>program crashes<\/strong> and the <strong>interpreter displays<\/strong> a <strong>runtime exception<\/strong> <strong>message<\/strong> on the <strong>screen<\/strong>.<\/p>\n<p>There are many <strong>different circumstances<\/strong> under which your <strong>Python program<\/strong> might <strong>crash<\/strong>.<\/p>\n<p>In fact, <strong>Python\u2019s interpreter<\/strong> comes up with a <strong>complete list<\/strong> of <strong>built-in <\/strong>exception <strong>types<\/strong>.<\/p>\n<h3>Common Python Exceptions<\/h3>\n<p>You\u2019ll find some of the common exceptions in the table below.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Exception<\/b><\/td>\n<td><b>Why it gets raised<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">AssertionError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When an <strong>assert statement<\/strong> <strong>fails<\/strong>.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">AttributeError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When an attribute <strong>reference<\/strong> or <strong>assignment<\/strong> fails.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">EOFError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When input() function encounters <strong>End Of File(EOF)<\/strong>.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">FloatingPointError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When an operation on <strong>floating-point<\/strong> fails.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">ImportError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When a module you want to <strong>import<\/strong> is not found.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">IndexError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When you try to access the <strong>element<\/strong> at an index which is out of range.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">KeyError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When a key is not present in a <strong>dictionary<\/strong>.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">KeyboardInterrupt<\/span><\/td>\n<td><span style=\"font-weight: 400\">When the user presses the interrupt key<strong>(Control-c or Delete)<\/strong>.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">MemoryError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When an operation runs out of <strong>memory<\/strong>.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">NameError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When a variable is not found in <strong>scope<\/strong>.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">NotImplementedError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When an <strong>abstract<\/strong> method is not implemented anywhere in the code.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">OSError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When a <strong>system-related<\/strong> error occurs.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">OverflowError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When an <strong>arithmetic operation<\/strong> produces a really large result<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">RuntimeError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When a <strong>run-time<\/strong> error doesn\u2019t fall in any of the other categories.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">StopIteration<\/span><\/td>\n<td><span style=\"font-weight: 400\">The <\/span><strong>next()<\/strong><span style=\"font-weight: 400\"> function raises it when there are no more elements in the iterable.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">SyntaxError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When there is an error in the <strong>syntax<\/strong>.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">IndentationError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When the <strong>indentation<\/strong> is not correct.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">TabError<\/span><\/td>\n<td><span style=\"font-weight: 400\">Inconsistent tabs and spaces in the indentation. It is a subclass of <\/span><strong>IndentationError<\/strong><span style=\"font-weight: 400\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">SystemError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When the interpreter detects an <strong>internal error<\/strong>.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">TypeError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When an operation or function tries to operate on an object of the <strong>incorrect type<\/strong>.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">UnboundLocalError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When you reference a local variable before <strong>assigning<\/strong> it a value.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">ValueError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When you pass an object of correct type but the <strong>improper<\/strong> value to a function.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">ZeroDivisionError<\/span><\/td>\n<td><span style=\"font-weight: 400\">When the second operand of a division or modulo operator is <strong>zero<\/strong>.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Summary<\/h3>\n<p>In this Python error article by TechVidan, we saw what <strong>Python error<\/strong> is and how it can affect the <strong>normal flow<\/strong> of <strong>execution<\/strong> of our <strong>program<\/strong>. We also learned about <strong>Python exceptions<\/strong> and their <strong>types<\/strong>.<\/p>\n<p>A <strong>good program<\/strong> should be able to <strong>handle<\/strong> any <strong>exception<\/strong> that may possibly <strong>occur<\/strong>. We\u2019ll learn how to <strong>handle<\/strong> these <strong>exceptions<\/strong> in another article.<\/p>\n<p>Till then, keep <strong>coding<\/strong> and be careful of those <strong>errors<\/strong>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>No matter how good you make your code, things can go wrong. When we plan our program, we always work for situations we normally expect. And our program works quite well in those situations.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79378,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3000,3001,3002],"class_list":["post-79304","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-errors-and-exceptions-in-python","tag-python-error","tag-python-exception"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Errors and Exceptions in Python - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn what is Python error with example, types of Errors in python-syntax errors and exception, types of exception and when they are raised.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Errors and Exceptions in Python - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn what is Python error with example, types of Errors in python-syntax errors and exception, types of exception and when they are raised.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-05T03:30:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/errors-exceptions-in-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Errors and Exceptions in Python - TechVidvan","description":"Learn what is Python error with example, types of Errors in python-syntax errors and exception, types of exception and when they are raised.","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:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Errors and Exceptions in Python - TechVidvan","og_description":"Learn what is Python error with example, types of Errors in python-syntax errors and exception, types of exception and when they are raised.","og_url":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-07-05T03:30:49+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/errors-exceptions-in-python.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Errors and Exceptions in Python","datePublished":"2020-07-05T03:30:49+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/"},"wordCount":800,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/errors-exceptions-in-python.jpg","keywords":["errors and exceptions in python","python error","python exception"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/","url":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/","name":"Errors and Exceptions in Python - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/errors-exceptions-in-python.jpg","datePublished":"2020-07-05T03:30:49+00:00","description":"Learn what is Python error with example, types of Errors in python-syntax errors and exception, types of exception and when they are raised.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/errors-exceptions-in-python.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/errors-exceptions-in-python.jpg","width":802,"height":420,"caption":"Python errors and exceptions"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/errors-and-exceptions-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Errors and Exceptions in Python"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=79304"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79304\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79378"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=79304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=79304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=79304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}