{"id":25528712,"date":"2022-06-17T16:54:14","date_gmt":"2022-06-17T11:24:14","guid":{"rendered":"https:\/\/entri.app\/blog\/?p=25528712"},"modified":"2023-05-03T13:07:38","modified_gmt":"2023-05-03T07:37:38","slug":"how-to-catch-all-exceptions-in-python-programming","status":"publish","type":"post","link":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/","title":{"rendered":"How to Catch All Exceptions in Python Programming"},"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-69d396f321fe1\" 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-69d396f321fe1\"  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\/how-to-catch-all-exceptions-in-python-programming\/#Exceptions_in_Python\" >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\/how-to-catch-all-exceptions-in-python-programming\/#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\/how-to-catch-all-exceptions-in-python-programming\/#Catching_Specific_Exceptions_in_Python\" >Catching Specific Exceptions in Python<\/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\/how-to-catch-all-exceptions-in-python-programming\/#Raising_Exceptions_in_Python\" >Raising Exceptions in Python<\/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\/how-to-catch-all-exceptions-in-python-programming\/#Python_try_with_else_clause\" >Python try with else clause<\/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\/how-to-catch-all-exceptions-in-python-programming\/#Python_try\" >Python try\u00a0<\/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\/how-to-catch-all-exceptions-in-python-programming\/#Advantages_of_Exception_Handling_and_Using_the_right_APM\" >Advantages of Exception Handling and Using the right APM<\/a><\/li><\/ul><\/nav><\/div>\n<header class=\"entry-header\">\n<p class=\"entry-title\"><span style=\"color: #333333; font-size: 15px;\">One of the struggles developers face is how to catch all Python exceptions. Developers often categorize exceptions as coding mistakes that lead to errors when running the program. Some developers still fail to distinguish between errors and exceptions.<\/span><\/p>\n<p><a href=\"https:\/\/entri.app\/course\/python-programming-course\/\" 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<\/header>\n<div class=\"entry-content content\">\n<p>In the case of Python application development, a python program terminates as soon as it encounters an unhandled error. So, in order to establish the difference between errors and exceptions, there are two types of errors:<\/p>\n<ul>\n<li>Syntax errors<\/li>\n<li>Logical errors (Exceptions)<\/li>\n<\/ul>\n<p style=\"text-align: center;\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Ready to take your python skills to the next level? Sign up for a free demo today!&#8221;<\/a><\/strong><\/p>\n<p>First, let\u2019s examine the syntax errors. Python syntax errors are caused by not following the proper structure (syntax) of the language. It is also known as a parsing error.<\/p>\n<p>Here\u2019s an example:<\/p>\n<p>&gt;&gt;&gt; ages = {<\/p>\n<p>\u2018jj\u2019: 2,<\/p>\n<p>\u2018yoyo\u2019: 4<\/p>\n<p>}<\/p>\n<p>print(f\u2019JJ is {ages[\u201cjj\u201d]} years old.\u2019)<\/p>\n<p>The output:<\/p>\n<p>JJ is 2 years old.<\/p>\n<p>This is a simple code with no syntax error. Then we will add another variable tomtom:<\/p>\n<p>&gt;&gt;&gt; ages = {<\/p>\n<p>\u2018jj\u2019: 2,<\/p>\n<p>\u2018yoyo\u2019: 4<\/p>\n<p>\u2018tomtom\u2019: 6<\/p>\n<p>}<\/p>\n<p>print(f\u2019JJ is {ages[\u201cjj\u201d]} years old.\u2019)<\/p>\n<p>Upon inspection, you can see an invalid syntax on the second entry, yoyo, inside the array with a missing comma. Try to run this code, and you will get a traceback:<\/p>\n<p>File \u201c&lt;pyshell&gt;\u201d, line 1<\/p>\n<p>&gt;&gt;&gt; ages = {<\/p>\n<p>^<\/p>\n<p>SyntaxError: invalid syntax<\/p>\n<p>As you notice, the traceback message didn\u2019t pinpoint the exact line where the syntax error occurs. The Python interpreter only attempts to locate where the invalid syntax is. It only points to where it first noticed the problem. So, when you get a SyntaxError traceback, it means that you should visually inspect whether the interpreter is pointing to the right error.<\/p>\n<p>In the example above, the interpreter encounters an invalid syntax inside an array called ages. Thus, it points out that there is something wrong inside the array.<\/p>\n<p>This is a syntax error.<\/p>\n<p>In most cases, a Python developer writes an impressive piece of code that is ready to execute. The program becomes a robust machine learning model, but during execution, Python throws up an unexpected error. Unfortunately, it is no longer the typical syntax error. Developers are now dealing with logical errors, also known as\u00a0<strong><em>exceptions<\/em><\/strong>.<\/p>\n<p style=\"text-align: center;\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Experience the power of our web development course with a free demo &#8211; enroll now!&#8221;<\/a><\/strong><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Exceptions_in_Python\"><\/span><strong>Exceptions in Python<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Exceptions are errors that occur at runtime. Mostly, these errors are logical. For example, when you try to divide a number by zero, you will get a ZeroDivisionError. When you open a file(read) that doesn\u2019t exist, you will receive FileNotFoundError. Further, when you try to import a module that doesn\u2019t exist, you will get ImportError.<\/p>\n<p>Here is how Python treats the above-mentioned errors:<\/p>\n<p><strong>ZeroDivisionError<\/strong><\/p>\n<p>&gt;&gt;&gt; 2 \/ 0<\/p>\n<p>Traceback (most recent call last):<\/p>\n<p>File \u201c&lt;pyshell&gt;\u201d, line 1, in &lt;module&gt;<\/p>\n<p>ZeroDivisionError: division by zero<\/p>\n<p><strong>FileNotFoundError<\/strong><\/p>\n<p>&gt;&gt;&gt; open(\u201cstack.txt\u201d)<\/p>\n<p>Traceback (most recent call last):<\/p>\n<p>File \u201c&lt;pyshell&gt;\u201d, line 1, in &lt;module&gt;<\/p>\n<p>FileNotFoundError: [Errno 2] No such file or directory: \u2018stack.txt\u2019<\/p>\n<p><strong>ImportError<\/strong><\/p>\n<p>&gt;&gt;&gt; from collections import qwerty<\/p>\n<p>Traceback (most recent call last):<\/p>\n<p>File \u201c&lt;pyshell&gt;\u201d, line 1, in &lt;module&gt;<\/p>\n<p>ImportError: cannot import name \u2018qwerty\u2019<\/p>\n<p>Whenever a runtime error occurs, Python creates an exception object. It then creates and prints a\u00a0<strong>traceback<\/strong>\u00a0of that error with details on why the error happened.<\/p>\n<p>Here are common exceptions in Python:<\/p>\n<ul>\n<li><strong>IndexError<\/strong>\u00a0\u2013 You will get this error when an index is not found in a sequence. For instance, accessing the 6th index when the length of the list is only five(5).<\/li>\n<li><strong>IndentationError<\/strong>\u00a0\u2013 Happens when indentation is not specified correctly.<\/li>\n<li><strong>ValueError<\/strong>\u00a0\u2013 Occurs when the built-in function for a data type has the valid type of arguments, but the arguments have invalid values.<\/li>\n<li><strong>IOError\u00a0<\/strong>\u2013 Developers often encounter this error when an input\/output operation fails.<\/li>\n<li><strong>Arithmetic Error<\/strong>\u00a0\u2013 Occurs when numeric calculations fail.<\/li>\n<li><strong>Floating-point Error<\/strong>\u00a0\u2013 Happens when a floating-point calculation fails.<\/li>\n<li><strong>Assertion Error<\/strong>\u00a0\u2013 Occurs when there is assert statement failure.<\/li>\n<li><strong>Overflow Error<\/strong>\u00a0\u2013 Developers get this error if the result of an arithmetic operation is too large and becomes machine unreadable.<\/li>\n<li><strong>Type Error<\/strong>\u00a0\u2013 Happens when an incorrect type of function or operation is applied to an object.<\/li>\n<\/ul>\n<p>You can visit the official Python documentation site to have in-depth knowledge about\u00a0Python built-in exceptions. While the Python community offers great support, Python application deployment discussions can be improved.\u00a0<strong>Application Performance Management (APM)<\/strong> is a great tool to deal with Python exceptions. It helps you find all the exceptions thrown and identify its root cause.<\/p>\n<p style=\"text-align: center;\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Get hands-on with our python course &#8211; sign up for a free demo!&#8221;<\/a><\/strong><\/p>\n<h2><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>A direct logic is followed to catch exceptions in Python. When an exception occurs, the Python interpreter stops the current process. It is handled by passing through the calling process. If not, the program will crash.<\/p>\n<p>For instance, a Python program has a function X that calls function Y, which in turn calls function Z. If an exception exists in function Z but is not handled within Z, the exception passes to Y and then to X. Simply, it will have a domino effect.<\/p>\n<p>An unhandled exception displays an error message and the program suddenly crashes. To avoid such a scenario, there are two methods to handle Python exceptions:<\/p>\n<ol>\n<li><strong>Try\u00a0<\/strong>\u2013 This method catches the exceptions raised by the program<\/li>\n<li><strong>Raise\u00a0<\/strong>\u2013 Triggers an exception manually using custom exceptions<\/li>\n<\/ol>\n<p>Let\u2019s start with the try statement to handle exceptions. Place the critical operation that can raise an exception inside the try clause. On the other hand, place the code that handles the exceptions in the except clause.<\/p>\n<p>Developers may choose what operations to perform once it catches these exceptions. Take a look at the sample code below:<\/p>\n<p>import sys<\/p>\n<p>list = [\u2018x\u2019, 1e-15, 5]\n<p>for result in list:<\/p>\n<p>try:<\/p>\n<p>print(\u201cThe result is\u201d, result)<\/p>\n<p>y = 1\/int(result)<\/p>\n<p>break<\/p>\n<p>except:<\/p>\n<p>print(\u201cWhew!\u201d, sys.exc_info()[0], \u201coccurred.\u201d)<\/p>\n<p>print(\u201cNext input please.\u201d)<\/p>\n<p>print()<\/p>\n<p>print(\u201cThe answer of\u201d, result, \u201cis\u201d, y)<\/p>\n<p>The program has an array called list with three elements. Next, the line that causes an exception is placed inside the try block. If there are no exceptions, the except block will skip, and the logic flow will continue until the last element. However, if an exception occurs, the except block will catch it.<\/p>\n<p>The output:<\/p>\n<p>The result is x<\/p>\n<p>Whew! &lt;class \u2018ValueError\u2019&gt; occurred.<\/p>\n<p>Next input, please.<\/p>\n<p>The result is 1e-15<\/p>\n<p>Whew! &lt;class \u2018ZeroDivisionError\u2019&gt; occurred.<\/p>\n<p>Next input, please.<\/p>\n<p>The result is 5<\/p>\n<p>The answer to 5 is 0.2<\/p>\n<p>To print the name of the exception, we use the exc_info()function inside the sys module. The first two elements cause exceptions since an integer can\u2019t be divided with string and a zero value. Hence, it raised\u00a0<em>ValueError<\/em>\u00a0and\u00a0<em>ZeroDivisionError<\/em>\u00a0exceptions.<\/p>\n<p style=\"text-align: center;\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Ready to take your python skills to the next level? Sign up for a free demo today!&#8221;<\/a><\/strong><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Catching_Specific_Exceptions_in_Python\"><\/span><strong>Catching Specific Exceptions in Python<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>What if you want to deal with a specific exception? In the previous example, it didn\u2019t mention any specific exception in the except block. That is not a good programming practice because it will catch all exceptions.<\/p>\n<p>Additionally, it will handle all exceptions in the same manner, which is an incorrect approach. Therefore, maximize the use of the except block and specify which exceptions it should catch.<\/p>\n<p>To execute this concept, we can use a tuple of values to specify multiple exceptions. Here is a sample pseudo-code:<\/p>\n<p>try:<\/p>\n<p>#do something<\/p>\n<p>#your statements<\/p>\n<p>pass<\/p>\n<p>except Exception_1:<\/p>\n<p>#handle Exception_1 and execute this block statement<\/p>\n<p>pass<\/p>\n<p>except Exception_2:<\/p>\n<p>handle Exception_2 and execute this block statement<\/p>\n<p>pass<\/p>\n<p>except Exception_3:<\/p>\n<p>#handle Exception_3 and execute this block statement<\/p>\n<p>pass<\/p>\n<p>except:<\/p>\n<p>#handles all other exceptions<\/p>\n<p>pass<\/p>\n<p style=\"text-align: center;\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Experience the power of our web development course with a free demo &#8211; enroll now!&#8221;<\/a><\/strong><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Raising_Exceptions_in_Python\"><\/span><strong>Raising Exceptions in Python<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Another way to catch all Python exceptions when it occurs during runtime is to use the raise keyword. It is a manual process where you can optionally pass values to the exception to clarify the reason why it was raised.<\/p>\n<p>&gt;&gt;&gt; raise IndexError<\/p>\n<p>Traceback (most recent call last):<\/p>\n<p>File \u201c&lt;pyshell&gt;\u201d, line 1, in &lt;module&gt;<\/p>\n<p>IndexError<\/p>\n<p>&gt;&gt;&gt; raise OverflowError(\u201cArithmetic operation is too large\u201d)<\/p>\n<p>Traceback (most recent call last):<\/p>\n<p>File \u201c&lt;pyshell&gt;\u201d, line 1, in &lt;module&gt;<\/p>\n<p>OverflowError: Arithmetic operation is too large<\/p>\n<p>Let\u2019s have a simple code to illustrate how raise keyword works:<\/p>\n<p>try:<\/p>\n<p>x = int(input(\u201cEnter a positive integer: \u201c))<\/p>\n<p>if x &lt;= 0:<\/p>\n<p>raise ValueError(\u201cIt is not a positive number!\u201d)<\/p>\n<p>except ValueError as val_e:<\/p>\n<p>print(val_e)<\/p>\n<p>The output:<\/p>\n<p>Enter a positive integer: -6<\/p>\n<p>It is not a positive number!<\/p>\n<p>Enter a positive integer: 6<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Python_try_with_else_clause\"><\/span><strong>Python try with else clause<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In dealing with exceptions, there are instances that a certain block of code inside try ran without any errors. As such, use the optional else keyword with the try statement.<\/p>\n<p>Here is a sample code:<\/p>\n<p>try:<\/p>\n<p>number = int(input(\u201cEnter a number: \u201c))<\/p>\n<p>assert number % 2 == 0<\/p>\n<p>except:<\/p>\n<p>print(\u201cThis is an odd number!\u201d)<\/p>\n<p>else:<\/p>\n<p>print(\u201cThis is an even number!\u201d)<\/p>\n<p>rem = number % 2<\/p>\n<p>print(\u201cThe remainder is \u201c, rem)<\/p>\n<p>The output:<\/p>\n<p>Enter a number: 3<\/p>\n<p>This is an odd number!<\/p>\n<p>Enter a number: 2<\/p>\n<p>This is an even number!<\/p>\n<p>The remainder is 0<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Python_try\"><\/span><strong>Python try\u00a0<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The try statement in Python has an optional finally block. It means that it executes the block by all means. At the same time, it releases external resources.<\/p>\n<p>Some examples include a connection between a mobile app and a remote data center via a distributed network. All resources should be clean once the program stops, whether it successfully runs or not.<\/p>\n<p>It is the job of the finally block to guarantee execution. Such actions include closing a file or GUI\u00a0 and disconnecting from the network.<\/p>\n<p>Check on the example below to illustrate how the operation works:<\/p>\n<p>try:<\/p>\n<p>file = open(\u201cmyFile.txt\u201d,encoding = \u2018utf-8\u2019)<\/p>\n<p># this performs file operations<\/p>\n<p>finally:<\/p>\n<p>f.close()<\/p>\n<p>The f.close()function ensures that the file is closed even if an exception happens during the program execution.<\/p>\n<p style=\"text-align: center;\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Get hands-on with our python course &#8211; sign up for a free demo!&#8221;<\/a><\/strong><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Advantages_of_Exception_Handling_and_Using_the_right_APM\"><\/span><strong>Advantages of Exception Handling and Using the right APM<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Bugs and errors are part of every developer\u2019s journey. As mentioned above, errors and exceptions are different. So, why is it that Python developers should know\u00a0how to catch Python exceptions\u00a0and master exception handling?<\/p>\n<p>The answers to this are not theoretical. Hence, let\u2019s learn with an example.<\/p>\n<p>For example, you\u2019re dealing with a vast amount of data. So, you build a program that reads thousands of files across multiple directories. There is no way that you will not encounter an error here. Possible errors may include wrong or missing file type, invalid file format, or dealing with different file extensions.<\/p>\n<p><a href=\"https:\/\/entri.app\/course\/python-programming-course\/\" 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<p>On that note, it is not feasible to open all the files and write a script that can cater to it accordingly. With\u00a0exception handling, developers can define multiple conditions. For instance, resolve an invalid format by establishing the correct format first and then reading the file afterward.<\/p>\n<p>Another option is to continue reading the data and skip those files with an invalid format.\u00a0 Then, create a log file that can deal with it later.\u00a0 To help you with log files, APMs like\u00a0Retrace is a great tool in dealing with logs.<\/p>\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<h4><strong>Related Articles<\/strong><\/h4>\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<table class=\"table\" dir=\"ltr\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<colgroup>\n<col width=\"329\" \/>\n<col width=\"309\" \/><\/colgroup>\n<tbody>\n<tr>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Syllabus&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-village-field-assistant-vfa-syllabus-exam-pattern\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/data-visualization-in-python\/\" target=\"_blank\" rel=\"noopener\">Introduction to Data Visualization in Python<\/a><\/strong><\/td>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Mock Test&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-free-mock-test\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/why-python-is-used-for-data-science\/\" target=\"_blank\" rel=\"noopener\">Why Python Is Used For Data Science?<\/a><\/strong><\/td>\n<\/tr>\n<tr>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Exam Date&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-exam-date\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/step-by-step-guide-for-getting-a-job-as-a-python-developer\/\" target=\"_blank\" rel=\"noopener\">Guide for getting a job as a Python Developer<\/a><\/strong><\/td>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Video Course&quot;}\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/top-python-interview-questions-and-answers\/\" target=\"_blank\" rel=\"noopener\">Python Advanced Interview Questions and Answers<\/a><\/strong><\/td>\n<\/tr>\n<tr>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Application Form&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-apply-online\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/python-online-course\/\" target=\"_blank\" rel=\"noopener\">Best Online Python Course with Certificate<\/a><\/strong><\/td>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Study Materials&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-study-material\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/type-conversion-in-python\/\" target=\"_blank\" rel=\"noopener\">What is Type Conversion in Python?<\/a><\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>One of the struggles developers face is how to catch all Python exceptions. Developers often categorize exceptions as coding mistakes that lead to errors when running the program. Some developers still fail to distinguish between errors and exceptions. In the case of Python application development, a python program terminates as soon as it encounters an [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":25529004,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[802,1888],"tags":[],"class_list":["post-25528712","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>How to Catch All Exceptions in Python Programming - Entri Blog<\/title>\n<meta name=\"description\" content=\"In this blog, we will learn about exceptions in Python . We will cover exceptions and different types of exceptions in Python.\" \/>\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\/how-to-catch-all-exceptions-in-python-programming\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Catch All Exceptions in Python Programming - Entri Blog\" \/>\n<meta property=\"og:description\" content=\"In this blog, we will learn about exceptions in Python . We will cover exceptions and different types of exceptions in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/\" \/>\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-17T11:24:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-03T07:37:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/How-to-Catch-All-Exceptions-in-Python-Programming.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=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/\"},\"author\":{\"name\":\"Feeba Mahin\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36\"},\"headline\":\"How to Catch All Exceptions in Python Programming\",\"datePublished\":\"2022-06-17T11:24:14+00:00\",\"dateModified\":\"2023-05-03T07:37:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/\"},\"wordCount\":1945,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/How-to-Catch-All-Exceptions-in-Python-Programming.png\",\"articleSection\":[\"Articles\",\"Python Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/\",\"url\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/\",\"name\":\"How to Catch All Exceptions in Python Programming - Entri Blog\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/How-to-Catch-All-Exceptions-in-Python-Programming.png\",\"datePublished\":\"2022-06-17T11:24:14+00:00\",\"dateModified\":\"2023-05-03T07:37:38+00:00\",\"description\":\"In this blog, we will learn about exceptions in Python . We will cover exceptions and different types of exceptions in Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#primaryimage\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/How-to-Catch-All-Exceptions-in-Python-Programming.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/How-to-Catch-All-Exceptions-in-Python-Programming.png\",\"width\":820,\"height\":615,\"caption\":\"How to Catch All Exceptions in Python Programming\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#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\":\"How to Catch All Exceptions in Python Programming\"}]},{\"@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":"How to Catch All Exceptions in Python Programming - Entri Blog","description":"In this blog, we will learn about exceptions in Python . We will cover exceptions and different types of exceptions in Python.","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\/how-to-catch-all-exceptions-in-python-programming\/","og_locale":"en_US","og_type":"article","og_title":"How to Catch All Exceptions in Python Programming - Entri Blog","og_description":"In this blog, we will learn about exceptions in Python . We will cover exceptions and different types of exceptions in Python.","og_url":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/","og_site_name":"Entri Blog","article_publisher":"https:\/\/www.facebook.com\/entri.me\/","article_published_time":"2022-06-17T11:24:14+00:00","article_modified_time":"2023-05-03T07:37:38+00:00","og_image":[{"width":820,"height":615,"url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/How-to-Catch-All-Exceptions-in-Python-Programming.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":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#article","isPartOf":{"@id":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/"},"author":{"name":"Feeba Mahin","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36"},"headline":"How to Catch All Exceptions in Python Programming","datePublished":"2022-06-17T11:24:14+00:00","dateModified":"2023-05-03T07:37:38+00:00","mainEntityOfPage":{"@id":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/"},"wordCount":1945,"commentCount":0,"publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"image":{"@id":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/How-to-Catch-All-Exceptions-in-Python-Programming.png","articleSection":["Articles","Python Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/","url":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/","name":"How to Catch All Exceptions in Python Programming - Entri Blog","isPartOf":{"@id":"https:\/\/entri.app\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#primaryimage"},"image":{"@id":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/How-to-Catch-All-Exceptions-in-Python-Programming.png","datePublished":"2022-06-17T11:24:14+00:00","dateModified":"2023-05-03T07:37:38+00:00","description":"In this blog, we will learn about exceptions in Python . We will cover exceptions and different types of exceptions in Python.","breadcrumb":{"@id":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#primaryimage","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/How-to-Catch-All-Exceptions-in-Python-Programming.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/How-to-Catch-All-Exceptions-in-Python-Programming.png","width":820,"height":615,"caption":"How to Catch All Exceptions in Python Programming"},{"@type":"BreadcrumbList","@id":"https:\/\/entri.app\/blog\/how-to-catch-all-exceptions-in-python-programming\/#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":"How to Catch All Exceptions in Python Programming"}]},{"@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\/25528712","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=25528712"}],"version-history":[{"count":9,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25528712\/revisions"}],"predecessor-version":[{"id":25559488,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25528712\/revisions\/25559488"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media\/25529004"}],"wp:attachment":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media?parent=25528712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/categories?post=25528712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/tags?post=25528712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}