{"id":25530260,"date":"2022-06-26T01:57:00","date_gmt":"2022-06-25T20:27:00","guid":{"rendered":"https:\/\/entri.app\/blog\/?p=25530260"},"modified":"2022-11-22T16:27:02","modified_gmt":"2022-11-22T10:57:02","slug":"what-is-try-except-in-python-and-how-can-we-use-it","status":"publish","type":"post","link":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/","title":{"rendered":"What is Try Except in Python and How can we Use it?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a058ff7c2663\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a058ff7c2663\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#What_are_exceptions_in_Python\" >What are exceptions in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#Catching_Exceptions_in_Python\" >Catching Exceptions in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#try-except\" >try-except<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#try_finally\" >try finally<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#try_else\" >try else<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#Raise_Exception\" >Raise Exception<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#Built-in_exceptions\" >Built-in exceptions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#_User-defined_Exceptions\" >\u00a0User-defined Exceptions<\/a><\/li><\/ul><\/nav><\/div>\n<section class=\"post-content\">The\u00a0<em>try except<\/em>\u00a0statement can handle exceptions. Exceptions may happen when you run a program.Exceptions are errors that happen during execution of the program. Python won\u2019t tell you about errors like syntax errors (grammar faults), instead it will abruptly stop.An abrupt exit is bad for both the end user and developer.Instead of an emergency halt, you can use a try except statement to properly deal with the problem. An emergency halt will happen if you do not properly handle exceptions.<\/p>\n<p><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-25522670 size-full\" src=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1.png\" alt=\"Python and Machine Learning Rectangle\" width=\"970\" height=\"250\" srcset=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1.png 970w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1-300x77.png 300w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1-768x198.png 768w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Rectangle-1-750x193.png 750w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/a><\/p>\n<h2 id=\"What-are-exceptions-in-Python\"><span class=\"ez-toc-section\" id=\"What_are_exceptions_in_Python\"><\/span><strong>What are exceptions in Python?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Python has built-in exceptions which can output an error. If an error occurs while running the program, it\u2019s called an exception.<\/p>\n<p>If an exception occurs, the type of exception is shown. Exceptions needs to be dealt with or the program will crash. To handle exceptions, the\u00a0<code>try-catch<\/code>\u00a0block is used.<\/p>\n<p>Some exceptions you may have seen before are\u00a0<code>FileNotFoundError<\/code>,\u00a0<code>ZeroDivisionError<\/code>\u00a0or\u00a0<code>ImportError<\/code>\u00a0but there are many more.<\/p>\n<p>All exceptions in Python inherit from the class BaseException. If you open the Python interactive shell and type the following statement it will list all built-in exceptions:<\/p>\n<div class=\"highlight sh\">\n<pre><code> &gt;&gt;&gt; dir(<strong>builtins<\/strong>)<\/code><\/pre>\n<\/div>\n<p>The idea of the try-except clause is to handle exceptions (errors at runtime). The syntax of the try-except block is:<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">try<\/span>:<\/span>\r\n<span class=\"line\">    &lt;do something&gt;<\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span> Exception:<\/span>\r\n<span class=\"line\">    &lt;handle the error&gt;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>The idea of the\u00a0<em>try-except block<\/em>\u00a0is this:<\/p>\n<ul>\n<li><strong>try<\/strong>: the code with the exception(s) to catch. If an exception is raised, it jumps straight into the except block.<\/li>\n<li><strong>except<\/strong>: this code is only executed\u00a0<em>if an exception occured<\/em>\u00a0in the try block. The except block is required with a try block, even if it contains only the pass statement.<\/li>\n<\/ul>\n<p>It may be combined with the\u00a0<strong>else<\/strong>\u00a0and\u00a0<strong>finally<\/strong>\u00a0keywords.<\/p>\n<ul>\n<li><strong>else<\/strong>: Code in the else block is only executed if no exceptions were raised in the try block.<\/li>\n<li><strong>finally<\/strong>: The code in the finally block is always executed, regardless of if a an exception was raised or not.<\/li>\n<\/ul>\n<p style=\"text-align: center;\"><strong><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\">Grab the opportunity to learn Python with Entri! Click Here<\/a><\/strong><\/p>\n<h2 id=\"Catching-Exceptions-in-Python\"><span class=\"ez-toc-section\" id=\"Catching_Exceptions_in_Python\"><\/span><strong>Catching Exceptions in Python<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The try-except block can handle exceptions. This prevents abrupt exits of the program on error. In the example below we purposely raise an exception.<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">try<\/span>: <\/span>\r\n<span class=\"line\">    <span class=\"number\">1<\/span> \/ <span class=\"number\">0<\/span><\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span> ZeroDivisionError: <\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'Divided by zero'<\/span>)<\/span>\r\n\r\n<span class=\"line\">print(<span class=\"string\">'Should reach here'<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>After the except block, the program continues. Without a try-except block, the last line wouldn\u2019t be reached as the program would crash.<\/p>\n<div class=\"highlight sh\">\n<pre><code> $ python3 example.py\r\n\r\nDivided by zero\r\nShould reach here<\/code><\/pre>\n<\/div>\n<p>In the above example we catch the specific exception ZeroDivisionError. You can handle any exception like this:<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">try<\/span>: <\/span>\r\n<span class=\"line\">    open(<span class=\"string\">\"fantasy.txt\"<\/span>)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span>: <\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'Something went wrong'<\/span>)<\/span>\r\n\r\n<span class=\"line\">print(<span class=\"string\">'Should reach here'<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>You can write different logic for each type of exception that happens:<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">try<\/span>: <\/span>\r\n<span class=\"line\">    <span class=\"comment\"># your code here<\/span><\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span> FileNotFoundError: <\/span>\r\n<span class=\"line\">    <span class=\"comment\"># handle exception <\/span><\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span> IsADirectoryError:<\/span>\r\n<span class=\"line\">    <span class=\"comment\"># handle exception<\/span><\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span>:<\/span>\r\n<span class=\"line\">    <span class=\"comment\"># all other types of exceptions<\/span><\/span>\r\n\r\n<span class=\"line\">print(<span class=\"string\">'Should reach here'<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2 id=\"try-except\"><span class=\"ez-toc-section\" id=\"try-except\"><\/span><strong>try-except<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let us do a real world example of the try-except block.<\/p>\n<p>The program asks for numeric user input. Instead the user types characters in the input box. The program normally would crash. But with a try-except block it can be handled properly.<\/p>\n<p>The\u00a0<em>try except<\/em>\u00a0statement prevents the program from crashing and properly deals with it.<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">try<\/span>:<\/span>\r\n<span class=\"line\">    x = input(<span class=\"string\">\"Enter number: \"<\/span>)<\/span>\r\n<span class=\"line\">    x = x + <span class=\"number\">1<\/span><\/span>\r\n<span class=\"line\">    print(x)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span>:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">\"Invalid input\"<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Entering invalid input, makes the program continue normally:<\/p>\n<p>try except<\/p>\n<p>The try except statement can be extended with the finally keyword, this will be executed if no exception is thrown:<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">finally<\/span>:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">\"Valid input.\"<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>The program continues execution if no exception has been thrown.<\/p>\n<p>There are different kinds of exceptions: ZeroDivisionError, NameError, TypeError and so on. Sometimes modules define their own exceptions.<\/p>\n<p>The try-except block works for function calls too:<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"function\"><span class=\"keyword\">def<\/span> <span class=\"title\">fail<\/span><span class=\"params\">()<\/span>:<\/span><\/span>\r\n<span class=\"line\">    <span class=\"number\">1<\/span> \/ <span class=\"number\">0<\/span><\/span>\r\n\r\n<span class=\"line\"><span class=\"keyword\">try<\/span>:<\/span>\r\n<span class=\"line\">    fail()<\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span>:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'Exception occured'<\/span>)<\/span>\r\n\r\n<span class=\"line\">print(<span class=\"string\">'Program continues'<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>&nbsp;<\/p>\n<p>This outputs:<\/p>\n<div class=\"highlight sh\">\n<pre><code> $ python3 example.py\r\n\r\nException occured\r\nProgram continues<\/code><\/pre>\n<\/div>\n<p style=\"text-align: center;\"><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\"><strong>Learn to code from industry experts! Enroll here<\/strong><\/a><\/p>\n<h2 id=\"try-finally\"><span class=\"ez-toc-section\" id=\"try_finally\"><\/span><strong>try finally<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A try-except block can have the finally clause (optionally). The finally clause is always executed.<br \/>\nSo the general idea is:<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">try<\/span>:<\/span>\r\n<span class=\"line\">    &lt;do something&gt;<\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span> Exception:<\/span>\r\n<span class=\"line\">    &lt;handle the error&gt;<\/span>\r\n<span class=\"line\"><span class=\"keyword\">finally<\/span>:<\/span>\r\n<span class=\"line\">    &lt;cleanup&gt;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>For instance: if you open a file you\u2019ll want to close it, you can do so in the finally clause.<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">try<\/span>: <\/span>\r\n<span class=\"line\">    f = open(<span class=\"string\">\"test.txt\"<\/span>)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span>: <\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'Could not open file'<\/span>)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">finally<\/span>:<\/span>\r\n<span class=\"line\">    f.close()<\/span>\r\n\r\n<span class=\"line\">print(<span class=\"string\">'Program continue'<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2 id=\"try-else\"><span class=\"ez-toc-section\" id=\"try_else\"><\/span><strong>try else<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The else clause is executed if and only if no exception is raised. This is different from the finally clause that\u2019s always executed.<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">try<\/span>:<\/span>\r\n<span class=\"line\">    x = <span class=\"number\">1<\/span><\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span>:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'Failed to set x'<\/span>)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">else<\/span>:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'No exception occured'<\/span>)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">finally<\/span>:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'We always do this'<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Output:<\/p>\n<div class=\"highlight sh\">\n<pre><code> No exception occured\r\n We always do this<\/code><\/pre>\n<\/div>\n<p>You can catch many types of exceptions this way, where\u00a0<em>the else clause<\/em>\u00a0is executed only if no exception happens.<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">try<\/span>:<\/span>\r\n<span class=\"line\">    lunch()<\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span> SyntaxError:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'Fix your syntax'<\/span>)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span> TypeError:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'Oh no! A TypeError has occured'<\/span>)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span> ValueError:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'A ValueError occured!'<\/span>)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">except<\/span> ZeroDivisionError:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'Did by zero?'<\/span>)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">else<\/span>:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'No exception'<\/span>)<\/span>\r\n<span class=\"line\"><span class=\"keyword\">finally<\/span>:<\/span>\r\n<span class=\"line\">    print(<span class=\"string\">'Ok then'<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2 id=\"Raise-Exception\"><span class=\"ez-toc-section\" id=\"Raise_Exception\"><\/span><strong>Raise Exception<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Exceptions are raised when an error occurs. But in Python you can also force an exception to occur with the keyword\u00a0<code>raise<\/code>.<\/p>\n<p>Any type of exception can be raised:<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"meta\">&gt;&gt;&gt; <\/span><span class=\"keyword\">raise<\/span> MemoryError(<span class=\"string\">\"Out of memory\"<\/span>)<\/span>\r\n<span class=\"line\">Traceback (most recent call last):<\/span>\r\n<span class=\"line\">  File <span class=\"string\">\"&lt;stdin&gt;\"<\/span>, line <span class=\"number\">1<\/span>, <span class=\"keyword\">in<\/span> &lt;module&gt;<\/span>\r\n<span class=\"line\">MemoryError: Out of memory<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"meta\">&gt;&gt;&gt; <\/span><span class=\"keyword\">raise<\/span> ValueError(<span class=\"string\">\"Wrong value\"<\/span>)<\/span>\r\n<span class=\"line\">Traceback (most recent call last):<\/span>\r\n<span class=\"line\">  File <span class=\"string\">\"&lt;stdin&gt;\"<\/span>, line <span class=\"number\">1<\/span>, <span class=\"keyword\">in<\/span> &lt;module&gt;<\/span>\r\n<span class=\"line\">ValueError: Wrong value<\/span>\r\n<span class=\"line\">&gt;&gt;&gt;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2 id=\"Built-in-exceptions\"><span class=\"ez-toc-section\" id=\"Built-in_exceptions\"><\/span><strong>Built-in exceptions<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A list of Python&#8217;s Built-in Exceptions is shown below. This list shows the Exception and why it is thrown (raised).<\/p>\n<table>\n<tbody>\n<tr>\n<th scope=\"col\">Exception<\/th>\n<th scope=\"col\">Cause of Error<\/th>\n<\/tr>\n<tr>\n<td>AssertionError<\/td>\n<td>if\u00a0<code>assert<\/code>\u00a0statement fails.<\/td>\n<\/tr>\n<tr>\n<td>AttributeError<\/td>\n<td>if attribute assignment or reference fails.<\/td>\n<\/tr>\n<tr>\n<td>EOFError<\/td>\n<td>if the\u00a0<code>input()<\/code>\u00a0functions hits end-of-file condition.<\/td>\n<\/tr>\n<tr>\n<td>FloatingPointError<\/td>\n<td>if a floating point operation fails.<\/td>\n<\/tr>\n<tr>\n<td>GeneratorExit<\/td>\n<td>Raise if a generator&#8217;s\u00a0<code>close()<\/code>\u00a0method is called.<\/td>\n<\/tr>\n<tr>\n<td>ImportError<\/td>\n<td>if the imported module is not found.<\/td>\n<\/tr>\n<tr>\n<td>IndexError<\/td>\n<td>if index of a sequence is out of range.<\/td>\n<\/tr>\n<tr>\n<td>KeyError<\/td>\n<td>if a key is not found in a dictionary.<\/td>\n<\/tr>\n<tr>\n<td>KeyboardInterrupt<\/td>\n<td>if the user hits interrupt key (Ctrl+c or delete).<\/td>\n<\/tr>\n<tr>\n<td>MemoryError<\/td>\n<td>if an operation runs out of memory.<\/td>\n<\/tr>\n<tr>\n<td>NameError<\/td>\n<td>if a variable is not found in local or global scope.<\/td>\n<\/tr>\n<tr>\n<td>NotImplementedError<\/td>\n<td>by abstract methods.<\/td>\n<\/tr>\n<tr>\n<td>OSError<\/td>\n<td>if system operation causes system related error.<\/td>\n<\/tr>\n<tr>\n<td>OverflowError<\/td>\n<td>if result of an arithmetic operation is too large to be represented.<\/td>\n<\/tr>\n<tr>\n<td>ReferenceError<\/td>\n<td>if a weak reference proxy is used to access a garbage collected referent.<\/td>\n<\/tr>\n<tr>\n<td>RuntimeError<\/td>\n<td>if an error does not fall under any other category.<\/td>\n<\/tr>\n<tr>\n<td>StopIteration<\/td>\n<td>by\u00a0<code>next()<\/code>\u00a0function to indicate that there is no further item to be returned by iterator.<\/td>\n<\/tr>\n<tr>\n<td>SyntaxError<\/td>\n<td>by parser if syntax error is encountered.<\/td>\n<\/tr>\n<tr>\n<td>IndentationError<\/td>\n<td>if there is incorrect indentation.<\/td>\n<\/tr>\n<tr>\n<td>TabError<\/td>\n<td>if indentation consists of inconsistent tabs and spaces.<\/td>\n<\/tr>\n<tr>\n<td>SystemError<\/td>\n<td>if interpreter detects internal error.<\/td>\n<\/tr>\n<tr>\n<td>SystemExit<\/td>\n<td>by\u00a0<code>sys.exit()<\/code>\u00a0function.<\/td>\n<\/tr>\n<tr>\n<td>TypeError<\/td>\n<td>if a function or operation is applied to an object of incorrect type.<\/td>\n<\/tr>\n<tr>\n<td>UnboundLocalError<\/td>\n<td>if a reference is made to a local variable in a function or method, but no value has been bound to that variable.<\/td>\n<\/tr>\n<tr>\n<td>UnicodeError<\/td>\n<td>if a Unicode-related encoding or decoding error occurs.<\/td>\n<\/tr>\n<tr>\n<td>UnicodeEncodeError<\/td>\n<td>if a Unicode-related error occurs during encoding.<\/td>\n<\/tr>\n<tr>\n<td>UnicodeDecodeError<\/td>\n<td>if a Unicode-related error occurs during decoding.<\/td>\n<\/tr>\n<tr>\n<td>UnicodeTranslateError<\/td>\n<td>if a Unicode-related error occurs during translating.<\/td>\n<\/tr>\n<tr>\n<td>ValueError<\/td>\n<td>if a function gets argument of correct type but improper value.<\/td>\n<\/tr>\n<tr>\n<td>ZeroDivisionError<\/td>\n<td>if second operand of division or modulo operation is zero.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: center;\"><strong><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\">Learn Coding in your Language! Enroll Here!<\/a><\/strong><\/p>\n<h2><span class=\"ez-toc-section\" id=\"_User-defined_Exceptions\"><\/span>\u00a0<strong>User-defined Exceptions<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Python has many standard types of exceptions, but they may not always serve your purpose.<br \/>\nYour program can have your own type of exceptions.<\/p>\n<p>To create a user-defined exception, you have to create a class that inherits from Exception.<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"class\"><span class=\"keyword\">class<\/span> <span class=\"title\">LunchError<\/span><span class=\"params\">(Exception)<\/span>:<\/span><\/span>\r\n<span class=\"line\">    <span class=\"keyword\">pass<\/span><\/span>\r\n\r\n<span class=\"line\"><span class=\"keyword\">raise<\/span> LunchError(<span class=\"string\">\"Programmer went to lunch\"<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>You made a user-defined exception named LunchError in the above code. You can raise this new exception if an error occurs.<\/p>\n<p>Outputs your custom error:<\/p>\n<div class=\"highlight sh\">\n<pre><code> $ python3 example.py\r\nTraceback (most recent call last):\r\n  File \u201cexample.py\u201d, line 5, in \r\n    raise LunchError(\u201cProgrammer went to lunch\u201d)\r\n<strong>main<\/strong>.LunchError: Programmer went to lunch<\/code><\/pre>\n<\/div>\n<p>Your program can have many user-defined exceptions. The program below throws exceptions based on a new projects money:<\/p>\n<figure class=\"highlight python\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"class\"><span class=\"keyword\">class<\/span> <span class=\"title\">NoMoneyException<\/span><span class=\"params\">(Exception)<\/span>:<\/span><\/span>\r\n<span class=\"line\">    <span class=\"keyword\">pass<\/span><\/span>\r\n\r\n<span class=\"line\"><span class=\"class\"><span class=\"keyword\">class<\/span> <span class=\"title\">OutOfBudget<\/span><span class=\"params\">(Exception)<\/span>:<\/span><\/span>\r\n<span class=\"line\">    <span class=\"keyword\">pass<\/span><\/span>\r\n\r\n<span class=\"line\">balance = int(input(<span class=\"string\">\"Enter a balance: \"<\/span>))<\/span>\r\n<span class=\"line\"><span class=\"keyword\">if<\/span> balance &lt; <span class=\"number\">1000<\/span>:<\/span>\r\n<span class=\"line\">    <span class=\"keyword\">raise<\/span> NoMoneyException<\/span>\r\n<span class=\"line\"><span class=\"keyword\">elif<\/span> balance &gt; <span class=\"number\">10000<\/span>:<\/span>\r\n<span class=\"line\">    <span class=\"keyword\">raise<\/span> OutOfBudget<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Here are some sample runs:<\/p>\n<div class=\"highlight sh\">\n<pre><code> $ python3 example.py\r\n    Enter a balance: 500\r\n    Traceback (most recent call last):\r\n      File \u201cexample.py\u201d, line 10, in \r\n        raise NoMoneyException\r\n    <strong>main<\/strong>.NoMoneyException<\/code><\/pre>\n<\/div>\n<div class=\"highlight sh\">\n<pre><code> $ python3 example.py\r\n    $ python3 example.py\r\n    Enter a balance: 100000\r\n    Traceback (most recent call last):\r\n      File \u201cexample.py\u201d, line 12, in \r\n        raise OutOfBudget\r\n    <strong>main<\/strong>.OutOfBudget<\/code><\/pre>\n<\/div>\n<p>It is a good practice to put all user-defined exceptions in a separate file (exceptions.py or errors.py). This is common practice in standard modules too.<\/p>\n<p><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-25520910 size-full\" src=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square.png\" alt=\"Python and Machine Learning Square\" width=\"345\" height=\"345\" srcset=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square.png 345w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-300x300.png 300w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-150x150.png 150w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-24x24.png 24w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-48x48.png 48w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-96x96.png 96w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/04\/Python-and-Machine-Learning-Square-75x75.png 75w\" sizes=\"auto, (max-width: 345px) 100vw, 345px\" \/><\/a><\/p>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0try except\u00a0statement can handle exceptions. Exceptions may happen when you run a program.Exceptions are errors that happen during execution of the program. Python won\u2019t tell you about errors like syntax errors (grammar faults), instead it will abruptly stop.An abrupt exit is bad for both the end user and developer.Instead of an emergency halt, you can [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":25530352,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[802,1888],"tags":[],"class_list":["post-25530260","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-python-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is Try Except in Python and How can we Use it? - Entri Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Try Except in Python and How can we Use it? - Entri Blog\" \/>\n<meta property=\"og:description\" content=\"The\u00a0try except\u00a0statement can handle exceptions. Exceptions may happen when you run a program.Exceptions are errors that happen during execution of the program. Python won\u2019t tell you about errors like syntax errors (grammar faults), instead it will abruptly stop.An abrupt exit is bad for both the end user and developer.Instead of an emergency halt, you can [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/\" \/>\n<meta property=\"og:site_name\" content=\"Entri Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/entri.me\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-25T20:27:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-22T10:57:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/What-is-Try-Except-in-Python-and-How-can-we-Use-it.png\" \/>\n\t<meta property=\"og:image:width\" content=\"820\" \/>\n\t<meta property=\"og:image:height\" content=\"615\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Feeba Mahin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@entri_app\" \/>\n<meta name=\"twitter:site\" content=\"@entri_app\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Feeba Mahin\" \/>\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:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/\"},\"author\":{\"name\":\"Feeba Mahin\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36\"},\"headline\":\"What is Try Except in Python and How can we Use it?\",\"datePublished\":\"2022-06-25T20:27:00+00:00\",\"dateModified\":\"2022-11-22T10:57:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/\"},\"wordCount\":1099,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/What-is-Try-Except-in-Python-and-How-can-we-Use-it.png\",\"articleSection\":[\"Articles\",\"Python Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/\",\"url\":\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/\",\"name\":\"What is Try Except in Python and How can we Use it? - Entri Blog\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/What-is-Try-Except-in-Python-and-How-can-we-Use-it.png\",\"datePublished\":\"2022-06-25T20:27:00+00:00\",\"dateModified\":\"2022-11-22T10:57:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#primaryimage\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/What-is-Try-Except-in-Python-and-How-can-we-Use-it.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/What-is-Try-Except-in-Python-and-How-can-we-Use-it.png\",\"width\":820,\"height\":615,\"caption\":\"What is Try Except in Python and How can we Use it\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/entri.app\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Programming\",\"item\":\"https:\/\/entri.app\/blog\/category\/python-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"What is Try Except in Python and How can we Use it?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/entri.app\/blog\/#website\",\"url\":\"https:\/\/entri.app\/blog\/\",\"name\":\"Entri Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/entri.app\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/entri.app\/blog\/#organization\",\"name\":\"Entri App\",\"url\":\"https:\/\/entri.app\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"width\":989,\"height\":446,\"caption\":\"Entri App\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/entri.me\/\",\"https:\/\/x.com\/entri_app\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36\",\"name\":\"Feeba Mahin\",\"url\":\"https:\/\/entri.app\/blog\/author\/feeba123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Try Except in Python and How can we Use it? - Entri Blog","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:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/","og_locale":"en_US","og_type":"article","og_title":"What is Try Except in Python and How can we Use it? - Entri Blog","og_description":"The\u00a0try except\u00a0statement can handle exceptions. Exceptions may happen when you run a program.Exceptions are errors that happen during execution of the program. Python won\u2019t tell you about errors like syntax errors (grammar faults), instead it will abruptly stop.An abrupt exit is bad for both the end user and developer.Instead of an emergency halt, you can [&hellip;]","og_url":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/","og_site_name":"Entri Blog","article_publisher":"https:\/\/www.facebook.com\/entri.me\/","article_published_time":"2022-06-25T20:27:00+00:00","article_modified_time":"2022-11-22T10:57:02+00:00","og_image":[{"width":820,"height":615,"url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/What-is-Try-Except-in-Python-and-How-can-we-Use-it.png","type":"image\/png"}],"author":"Feeba Mahin","twitter_card":"summary_large_image","twitter_creator":"@entri_app","twitter_site":"@entri_app","twitter_misc":{"Written by":"Feeba Mahin","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#article","isPartOf":{"@id":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/"},"author":{"name":"Feeba Mahin","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36"},"headline":"What is Try Except in Python and How can we Use it?","datePublished":"2022-06-25T20:27:00+00:00","dateModified":"2022-11-22T10:57:02+00:00","mainEntityOfPage":{"@id":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/"},"wordCount":1099,"commentCount":0,"publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"image":{"@id":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/What-is-Try-Except-in-Python-and-How-can-we-Use-it.png","articleSection":["Articles","Python Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/","url":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/","name":"What is Try Except in Python and How can we Use it? - Entri Blog","isPartOf":{"@id":"https:\/\/entri.app\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#primaryimage"},"image":{"@id":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/What-is-Try-Except-in-Python-and-How-can-we-Use-it.png","datePublished":"2022-06-25T20:27:00+00:00","dateModified":"2022-11-22T10:57:02+00:00","breadcrumb":{"@id":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#primaryimage","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/What-is-Try-Except-in-Python-and-How-can-we-Use-it.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/What-is-Try-Except-in-Python-and-How-can-we-Use-it.png","width":820,"height":615,"caption":"What is Try Except in Python and How can we Use it"},{"@type":"BreadcrumbList","@id":"https:\/\/entri.app\/blog\/what-is-try-except-in-python-and-how-can-we-use-it\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/entri.app\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Programming","item":"https:\/\/entri.app\/blog\/category\/python-programming\/"},{"@type":"ListItem","position":3,"name":"What is Try Except in Python and How can we Use it?"}]},{"@type":"WebSite","@id":"https:\/\/entri.app\/blog\/#website","url":"https:\/\/entri.app\/blog\/","name":"Entri Blog","description":"","publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/entri.app\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/entri.app\/blog\/#organization","name":"Entri App","url":"https:\/\/entri.app\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","width":989,"height":446,"caption":"Entri App"},"image":{"@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/entri.me\/","https:\/\/x.com\/entri_app"]},{"@type":"Person","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36","name":"Feeba Mahin","url":"https:\/\/entri.app\/blog\/author\/feeba123\/"}]}},"_links":{"self":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25530260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/comments?post=25530260"}],"version-history":[{"count":5,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25530260\/revisions"}],"predecessor-version":[{"id":25547714,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25530260\/revisions\/25547714"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media\/25530352"}],"wp:attachment":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media?parent=25530260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/categories?post=25530260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/tags?post=25530260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}