{"id":75924,"date":"2020-02-01T10:42:12","date_gmt":"2020-02-01T05:12:12","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75924"},"modified":"2020-02-01T10:42:12","modified_gmt":"2020-02-01T05:12:12","slug":"python-exception-handling","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/","title":{"rendered":"Python Exception Handling &#8211; Learn errors and exceptions in Python"},"content":{"rendered":"<p>In this <strong>Python exception<\/strong> <strong>handling article<\/strong>, we will talk about <strong>errors<\/strong> and <strong>exceptions<\/strong> in <strong>Python<\/strong>. We will also see how to <strong>raise<\/strong> and <strong>catch<\/strong> them. We will also learn about <strong>different keywords<\/strong> used for <strong>exception handling<\/strong> in <strong>Python<\/strong>.<\/p>\n<h3>What are Exceptions in Python Exception Handling?<\/h3>\n<p>Python is an <strong>interpreted language<\/strong>. When you are <strong>coding<\/strong> in the <strong>Python interpreter<\/strong>, you often have to deal with <strong>runtime errors<\/strong>. These are <strong>runtime exceptions<\/strong>.<\/p>\n<p>When there is an exception, all <strong>execution stops<\/strong> and it <strong>displays<\/strong> a red <strong>error<\/strong> message on the <strong>screen<\/strong>. But if we can handle it, the <strong>program<\/strong> will <strong>not crash<\/strong>. So an exception is when something goes wrong and your program <strong>cannot run<\/strong> anymore.<\/p>\n<p>Python has <strong>built-in<\/strong> exceptions but you can also <strong>define<\/strong> your <strong>own exceptions<\/strong>. This <strong>code<\/strong> raises an <strong>exception<\/strong>:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(1\/0)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#213&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>print(1\/0)<br \/>\nZeroDivisionError: division by zero<\/div>\n<p>It is <strong>not possible<\/strong> to <strong>divide<\/strong> <strong>1<\/strong> by <strong>0<\/strong>, so the program has to <strong>stop<\/strong>, it <strong>cannot execute<\/strong> anymore. So it raises a <strong>ZeroDivisionError<\/strong>. This is an <strong>in-built<\/strong> exception in <strong>Python<\/strong>.<\/p>\n<p>All exceptions <strong>inherit<\/strong> from the <strong>Exception base class<\/strong>.<\/p>\n<h3>The try-except Blocks in Python Exception Handling<\/h3>\n<p>If you know an operation can <strong>raise<\/strong> an <strong>exception<\/strong>, you can put it in a <strong>try-block<\/strong>. Like the <strong>if-statement<\/strong>, <strong>indentation<\/strong> is <strong>mandatory<\/strong> for the <strong>try-block<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3]\n&gt;&gt;&gt; nums[3]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#216&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>nums[3]<br \/>\nIndexError: list index out of range<\/div>\n<p>Here, the<strong> list nums<\/strong> only has <strong>3 items<\/strong>. So they must have <strong>indices<\/strong> <strong>0<\/strong> to <strong>2<\/strong>.<\/p>\n<p>If we try to <strong>access<\/strong> the <strong>item<\/strong> at <strong>index 3<\/strong>, it will <strong>raise<\/strong> an <strong>IndexError<\/strong> and the <strong>program<\/strong> will <strong>stop<\/strong>. But if we catch this <strong>exception<\/strong> in the <strong>except block<\/strong>, the <strong>program<\/strong> will <strong>continue running<\/strong> from the <strong>next statement<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; try:\n  nums[3]\nexcept:\n  print('Out of index!')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Out of index!<\/div>\n<p>Now whatever is after these <strong>4 lines<\/strong> will be <strong>executed normally<\/strong>. In the <strong>except block<\/strong>, we can put anything &#8211; <strong>assignment<\/strong>, <strong>function calls<\/strong>, or <strong>anything<\/strong> else. We can also get the <strong>exception<\/strong> that occurred by <strong>importing<\/strong> <strong>sys<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import sys\n&gt;&gt;&gt; try:\n  nums[3]\nexcept:\n  print('Out of index!')\n  print(sys.exc_info())<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Out of index!<br \/>\n(&lt;class &#8216;IndexError&#8217;&gt;, IndexError(&#8216;list index out of range&#8217;), &lt;traceback object at 0x00000289E33498C8&gt;)<\/div>\n<h3>Catching Specific Exceptions in Python Exception Handling<\/h3>\n<p>This catches any <strong>exception<\/strong> in the same way.<\/p>\n<p>If you know which <strong>exception<\/strong> will be raised by a <strong>piece of code<\/strong>, you can <strong>specifically<\/strong> catch that <strong>exception<\/strong> by <strong>mentioning<\/strong> it with <strong>except<\/strong>. We can have any <strong>number<\/strong> of <strong>except clauses<\/strong> after a try <strong>clause<\/strong>, but only <strong>one generic<\/strong> except <strong>clause<\/strong> in the end.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; try:\n  pass\nexcept ZeroDivisionError\nSyntaxError: invalid syntax\n&gt;&gt;&gt; try:\n  pass\nexcept ZeroDivisionError:\n  pass\nexcept (IndexError, NameError):\n  pass\nexcept:\n  pass<\/pre>\n<p>If you want to specify <strong>more than one exception<\/strong> in an <strong>except clause<\/strong>, you need to write it in a <strong>tuple<\/strong>.<\/p>\n<h3>The Finally Clause in Python Exception Handling<\/h3>\n<p>Sometimes even if an <strong>exception<\/strong> is <strong>raised<\/strong>, you may want to do something &#8211; like <strong>close a file<\/strong>.<\/p>\n<p>You can put a <strong>finally clause<\/strong> after the last <strong>except<\/strong> and then put the <strong>code<\/strong> in this &#8211; the code you want to <strong>execute<\/strong> no matter what.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; try:\n  f=open('text.txt')\n  f.write('Hello')\nexcept:\n  pass\nfinally:\n  f.close()<\/pre>\n<p>In this <strong>code<\/strong>, it <strong>opens<\/strong> a <strong>file text.txt<\/strong>, tries to write<strong> \u2018Hello\u2019<\/strong> to the file.<\/p>\n<p>It raises an <strong>exception<\/strong> because we <strong>opened<\/strong> the <strong>file <\/strong>in <strong>read-mode<\/strong> and are trying to <strong>write<\/strong> to it. But we don\u2019t handle this <strong>exception<\/strong>. Finally, we want to <strong>close<\/strong> this <strong>file<\/strong>. The code in the <strong>finally-block<\/strong> does this.<\/p>\n<h3>Raising Exceptions in Python<\/h3>\n<p><strong>Python<\/strong> raises an exception when there is a <strong>problem<\/strong>, but we can also <strong>forcefully raise<\/strong> an <strong>exception<\/strong>. We do this with the <strong>raise keyword<\/strong>. Unlike this, <strong>Java<\/strong> has the <strong>throw keyword<\/strong> for this.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; raise OSError<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#246&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>raise OSError<br \/>\nOSError<\/div>\n<p>This <strong>code raises<\/strong> an <strong>OSError<\/strong>.<\/p>\n<p>This was in the <strong>interpreter<\/strong> but we can put this anywhere in our <strong>code<\/strong>.<\/p>\n<ul>\n<li>We can also raise a <strong>RuntimeError<\/strong>.<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; raise<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#247&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>raise<br \/>\nRuntimeError: No active exception to reraise<\/div>\n<ul>\n<li>You can tell it which <strong>exception<\/strong> to <strong>raise<\/strong> along with a <strong>description<\/strong> of it.<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; raise OSError('There was an OS Error')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#248&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>raise OSError(&#8216;There was an OS Error&#8217;)<br \/>\nOSError: There was an OS Error<\/div>\n<p>This gives us a bit of <strong>description<\/strong> about the <strong>exception<\/strong> that occurred.<\/p>\n<h3>else with try-except<\/h3>\n<p>You can follow a <strong>try-except block<\/strong> with an <strong>else-block<\/strong>. If no <strong>exception<\/strong> is <strong>raised<\/strong> in the <strong>try-block<\/strong>, the <strong>code<\/strong> in the <strong>else-block executes<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; try:\n  print(1)\nexcept:\n  print(2)\nelse:\n  print(3)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n3<\/div>\n<h3>Standard Exceptions in Python Exception Handling<\/h3>\n<p>Before learning to <strong>create<\/strong> our <strong>own exceptions<\/strong>, let\u2019s see some<strong> in-built<\/strong> exceptions in <strong>Python<\/strong>.<\/p>\n<ul>\n<li>Exception &#8211; <strong>Base class<\/strong> for all exceptions<\/li>\n<li>StopIteration &#8211; When an <strong>iterator ends<\/strong><\/li>\n<li>SystemExit &#8211; Raised by <strong>sys.exit()<\/strong><\/li>\n<li>StandardError &#8211; Base class for in-built exceptions except <strong>StopIteration<\/strong> and <strong>SystemExit<\/strong><\/li>\n<li>ArithmeticError &#8211; Base class for arithmetic <strong>calculation errors<\/strong><\/li>\n<li>OverFlowError &#8211; When a calculation exceeds the <strong>maximum limit<\/strong><\/li>\n<li>FloatingPointError &#8211; When a <strong>floating-point<\/strong> operation fails<\/li>\n<li>ZeroDivisionError &#8211; When there is <strong>division<\/strong> or <strong>modulus<\/strong> by <strong>0<\/strong><\/li>\n<li>AssertionError &#8211; When an <strong>assert statement<\/strong> fails<\/li>\n<li>AttributeError &#8211; When an <strong>attribute reference<\/strong> or <strong>assignment fails<\/strong><\/li>\n<li>EOFError &#8211; On reaching the end of the <strong>file<\/strong> with no input from <strong>input()<\/strong><\/li>\n<li>ImportError &#8211; When an import <strong>statement fails<\/strong><\/li>\n<li>KeyboardInterrupt &#8211; When you interrupt execution by pressing <strong>Ctrl+C<\/strong><\/li>\n<li>LookupError &#8211; Base class for <strong>lookup errors<\/strong><\/li>\n<li>IndexError &#8211; When an <strong>index<\/strong> is not in a <strong>sequence<\/strong><\/li>\n<li>KeyError &#8211; When a <strong>key<\/strong> is <strong>not present<\/strong> in a <strong>dictionary<\/strong><\/li>\n<li>NameError &#8211; When an <strong>identifier<\/strong> is <strong>not present<\/strong> in the <strong>local\/global namespace<\/strong><\/li>\n<li>UnboundLocalError &#8211; When we access a <strong>local variable<\/strong> which hasn&#8217;t been assigned yet<\/li>\n<li>EnvironmentError &#8211; <strong>Base class<\/strong> for <strong>exceptions<\/strong> outside the Python environment<\/li>\n<li>IOError &#8211; When an<strong> input\/output<\/strong> operation fails<\/li>\n<li>OSError &#8211; For operating <strong>system-related errors<\/strong><\/li>\n<li>SyntaxError &#8211; When the <strong>syntax<\/strong> is <strong>incorrect<\/strong><\/li>\n<li>IndentationError &#8211; When there is <strong>not proper indentation<\/strong><\/li>\n<li>SystemError &#8211; When the interpreter has an <strong>internal problem<\/strong><\/li>\n<li>SystemExit &#8211; When the interpreter quits because of <strong>sys.exit()<\/strong><\/li>\n<li>TypeError &#8211; When an operation is <strong>invalid<\/strong> for <strong>specific data type<\/strong><\/li>\n<li>ValueError &#8211; When the <strong>in-built<\/strong> function has a <strong>valid type<\/strong> of arguments but <strong>invalid values<\/strong><\/li>\n<li>RuntimeError &#8211; When an <strong>error<\/strong> doesn&#8217;t fall in any other category<\/li>\n<li>NotImplementedError &#8211; When an <strong>abstract method<\/strong> is not <strong>implemented<\/strong> in an <strong>inherited class<\/strong><\/li>\n<\/ul>\n<h3>Defining our own exceptions in Python Exception Handling<\/h3>\n<p>Until now, we talked about <strong>in-built exceptions<\/strong> in Python. Now, let\u2019s learn to define our <strong>own exceptions<\/strong>.<\/p>\n<p>For this, you should:<\/p>\n<p><strong>1.<\/strong> Create a <strong>class<\/strong><\/p>\n<p><strong>2.<\/strong> Make it <strong>inherit<\/strong> from <strong>Exception<\/strong> or <strong>another exception class<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class InvalidTemperature(Exception):\n  def __init__(self, temp):\n    self.temp=temp\n  def __str__(self):\n    return f'The temperature {self.temp} is low'<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; raise InvalidTemperature(30)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#283&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>raise InvalidTemperature(30)<br \/>\nInvalidTemperature: The temperature 30 is low<\/div>\n<p>We can catch this exception by using <strong>try-except blocks<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; try:\n  raise InvalidTemperature(30)\nexcept:\n  print('Ok')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Ok<\/div>\n<h3>AssertionError in Python<\/h3>\n<p>Let\u2019s also talk about <strong>AssertionError<\/strong>.<\/p>\n<p>But first, let\u2019s talk about <strong>assert statements<\/strong>. The<strong> assert statement<\/strong> takes a <strong>condition<\/strong> that has to be <strong>true<\/strong>. If it isn&#8217;t, it raises an <strong>AssertionError<\/strong> and the <strong>program stops executing<\/strong>.<\/p>\n<p>So if the condition is <strong>True<\/strong>, the <strong>program<\/strong> <strong>continues running<\/strong>. But if it\u2019s not, it stops and raises an <strong>AssertionError exception<\/strong>. This is an <strong>in-built exception<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; marks=-4\n&gt;&gt;&gt; assert (marks&gt;=0 and marks&lt;=100)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#308&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>assert (marks&gt;=0 and marks&lt;=100)<br \/>\nAssertionError<\/div>\n<p>If marks are<strong> -4<\/strong> and we have an <strong>assert statement<\/strong> that says it should be in <strong>0<\/strong> to <strong>100<\/strong>, it raises an <strong>AssertionError<\/strong>.<\/p>\n<p>We can also give it a <strong>description<\/strong> for the <strong>error<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; assert (marks&gt;=0 and marks&lt;=100), 'Marks should be &gt;=0 and &lt;=100'<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#309&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>assert (marks&gt;=0 and marks&lt;=100), &#8216;Marks should be &gt;=0 and &lt;=100&#8217;<br \/>\nAssertionError: Marks should be &gt;=0 and &lt;=100<\/div>\n<h3>Summary<\/h3>\n<p>So today we talked about Python <strong>exception handling<\/strong>.<\/p>\n<p>We saw <strong>try-except blocks<\/strong>, catching <strong>specific exceptions<\/strong>, the <strong>finally clause<\/strong>, <strong>raising exceptions<\/strong>, <strong>standard exceptions<\/strong>, <strong>custom exceptions<\/strong>, and <strong>AssertionError<\/strong> in Python <strong>exception handling<\/strong>.<\/p>\n<p>This was all about TechVidvan&#8217;s Python <strong>exception<\/strong> handling article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python exception handling article, we will talk about errors and exceptions in Python. We will also see how to raise and catch them. We will also learn about different keywords used for&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":76115,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1532,1533,1534,1535,1536,1537,1538,1539],"class_list":["post-75924","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-assertionerror-in-python","tag-catching-specific-exceptions","tag-exception-handling-in-python","tag-python-exception-handling","tag-raising-exceptions-in-python","tag-the-finally-clause","tag-the-try-except-blocks","tag-what-are-exceptions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Exception Handling - Learn errors and exceptions in Python - TechVidvan<\/title>\n<meta name=\"description\" content=\"Explore what are exceptions in Python, catching specific exceptions in Python exception handling, the finally clause, assertionerror, standard exceptions, etc.\" \/>\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\/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 Exception Handling - Learn errors and exceptions in Python - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Explore what are exceptions in Python, catching specific exceptions in Python exception handling, the finally clause, assertionerror, standard exceptions, etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/\" \/>\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-02-01T05:12:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/python-exception-handling.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=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Exception Handling - Learn errors and exceptions in Python - TechVidvan","description":"Explore what are exceptions in Python, catching specific exceptions in Python exception handling, the finally clause, assertionerror, standard exceptions, etc.","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\/python-exception-handling\/","og_locale":"en_US","og_type":"article","og_title":"Python Exception Handling - Learn errors and exceptions in Python - TechVidvan","og_description":"Explore what are exceptions in Python, catching specific exceptions in Python exception handling, the finally clause, assertionerror, standard exceptions, etc.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-02-01T05:12:12+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/python-exception-handling.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":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Exception Handling &#8211; Learn errors and exceptions in Python","datePublished":"2020-02-01T05:12:12+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/"},"wordCount":1316,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/python-exception-handling.jpg","keywords":["assertionerror in python","Catching Specific Exceptions","exception handling in python","python exception handling","Raising Exceptions in python","The Finally Clause","The try-except Blocks","What are Exceptions"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/","url":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/","name":"Python Exception Handling - Learn errors and exceptions in Python - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/python-exception-handling.jpg","datePublished":"2020-02-01T05:12:12+00:00","description":"Explore what are exceptions in Python, catching specific exceptions in Python exception handling, the finally clause, assertionerror, standard exceptions, etc.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/python-exception-handling.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/python-exception-handling.jpg","width":802,"height":420,"caption":"exception handling in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-exception-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Exception Handling &#8211; Learn 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\/75924","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=75924"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75924\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/76115"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}