{"id":73474,"date":"2023-07-18T07:13:55","date_gmt":"2023-07-18T07:13:55","guid":{"rendered":"https:\/\/itsourcecode.com\/?p=73474"},"modified":"2023-11-23T04:21:59","modified_gmt":"2023-11-23T04:21:59","slug":"python-exception-handling-try-except-else-finally","status":"publish","type":"post","link":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/","title":{"rendered":"Python Exception Handling Best Practices"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-what-is-exception-handling-in-python\"><strong>What is Exception Handling in Python?<\/strong><\/h2>\n\n\n\n<p>Exception handling in Python refers to the process of anticipating and handling errors or exceptional events that may occur during the execution of a program.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>It allows developers to gracefully handle and recover from errors, ensuring that the program continues to run without abruptly terminating.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example\"><strong>Example<\/strong>:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 7\nb = 0\n\nprint (a\/b)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Traceback (most recent call last):\nFile \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\n\nZeroDivisionError: division by zero<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>The system can&#8217;t divide the number by zero in this code, so an exception is thrown.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-exception-handling-best-practices\"><strong>Python Exception Handling Best Practices<\/strong><\/h2>\n\n\n\n<p>When it comes to exception handling in Python, there are several best practices that can help you write robust and maintainable code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here are some of the key recommendations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Be specific with exceptions.<\/li>\n\n\n\n<li>Use multiple except blocks.<\/li>\n\n\n\n<li>Handle exceptions gracefully.<\/li>\n\n\n\n<li>Avoid silent failures.<\/li>\n\n\n\n<li>Use the finally block for cleanup.<\/li>\n\n\n\n<li>Use context managers for resource handling.<\/li>\n\n\n\n<li>Avoid bare except statements.<\/li>\n\n\n\n<li>Use custom exception classes.<\/li>\n\n\n\n<li>Log exceptions for debugging.<\/li>\n\n\n\n<li>Test exception scenarios.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Python try, except, else, and finally<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Try<\/strong>: This block will check for the error that was expected to happen.<\/li>\n\n\n\n<li><strong>Except<\/strong>: This block is where you can take care of the mistake.<\/li>\n\n\n\n<li><strong>Else<\/strong>: If there are no further errors, this block will be executed.<\/li>\n\n\n\n<li><strong>Finally<\/strong>: Whether an exception is made or not, the finally block is always executed<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Try\/Except Blocks in Python<\/strong><\/h3>\n\n\n\n<p>If you think your code might throw an exception, put it in a <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">try<\/mark><\/em> block.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n     print(x)\nexcept:\n     print(\"An exception occurred.\")<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>An exception occurred.<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>The <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">except<\/mark><\/em> block will be executed because the <mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\"><em>try<\/em><\/mark> block raised an error. If there was no <mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\"><em>try<\/em><\/mark> block, the program would crash and raise an error.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Python Multiple Excepts<\/strong><\/h4>\n\n\n\n<p>There can be multiple <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">except <\/mark><\/em>blocks for a single <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#eb0000\" class=\"has-inline-color\">try <\/mark><\/em>block.<\/p>\n\n\n\n<p>Let&#8217;s look at some examples of how Python handles more than one exception.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    print(x)\n\nexcept NameError:\n    print(\"Variable x is not defined\")\n\nexcept:\n    print(\"Something else went wrong\")\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Variable x is not defined<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>The interpreter checks the <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">except <\/mark><\/em>blocks to see if any of them can handle the exception when it encounters one. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In our example, the first statement under the <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">try <\/mark><\/em>block resulted in a <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">NameError<\/mark><\/em>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>The <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">try <\/mark><\/em>block contains code that may cause an error. The <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">except <\/mark><\/em>block contains code that will be executed if an error occurs. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>If an error occurs, the code in the <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">try <\/mark><\/em>block after the error will not be executed.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>If an appropriate <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fe0000\" class=\"has-inline-color\">except <\/mark><\/em>block or a generic <mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\"><em>except <\/em><\/mark>block is not found, the exception will go unhandled.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-python-multiple-exception-in-one-except\"><strong>Python Multiple Exception in one Except<\/strong><\/h4>\n\n\n\n<p>You can also have one <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">except <\/mark><\/em>block handle multiple exceptions by using parentheses.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n      print('50'+20)\n      print(7\/0)\nexcept (TypeError,ZeroDivisionError):\n      print(\"Invalid input\")<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Invalid input<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Try\/Else in Python<\/strong><\/h3>\n\n\n\n<p>Using the <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">else <\/mark><\/em>clause, you can specify a code block to be performed if no errors occur:<\/p>\n\n\n\n<p>Example: <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">try <\/mark><\/em>block does not produce an error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n      print(\"Python is Fun!\")\nexcept:\n      print(\"There's an error\")\nelse:\n      print(\"No error\")<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Try\/Except\/Finally Block in Python<\/strong><\/h3>\n\n\n\n<p>You can include a <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">finally<\/mark><\/em> clause exception block after the last <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">except<\/mark><\/em> block. This code will execute no matter what.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n     print(7\/0)\n\nexcept ValueError:\n     print('Value Error is raised')\n\nfinally:\n     print('Prints the finally block')<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Prints the finally block\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 2, in &lt;module&gt;\nZeroDivisionError: division by zero<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Raise Exceptions<\/strong><\/h2>\n\n\n\n<p>You might need to throw an exception to handle a situation. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>You can throw an exception by using the <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">raise <\/mark><\/em>keyword.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = -6\n\nif a &lt; 0:\n  raise Exception(\"Please input absolute values.\")<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Raise Without a Specified Exception in Python<\/strong><\/h3>\n\n\n\n<p>You can use the <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">raise <\/mark><\/em>keyword without specifying an exception, which will cause the original exception to be raised again. This can only be done inside an <em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">except <\/mark><\/em>block.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n         print('70'+10)\n\nexcept:\n         raise<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Traceback (most recent call last):\n         File \"&lt;stdin&gt;\", line 2, in &lt;module&gt;\nTypeError: can only concatenate str (not \"int\") to str<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Raise With an Argument in Python<\/strong><\/h3>\n\n\n\n<p>You can also include an argument with the exception you&#8217;re raising, to give additional details about what happened.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>raise ValueError(\"Enter a appropriate value\")<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Traceback (most recent call last):\n         File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nValueError: Enter a appropriate value<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Python Built-in Exceptions<\/strong><\/h2>\n\n\n\n<p>Below is the list of all the Built-in Exceptions in Python. You can visit the Python official documentation about the <a href=\"https:\/\/docs.python.org\/3\/library\/exceptions.html\"><strong>Standard Exception<\/strong><\/a> if you want more information.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Exception Name<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><strong>Exception<\/strong><\/td><td>Base class for all exceptions<\/td><\/tr><tr><td><strong>StopIteration<\/strong><\/td><td>Thrown when an iterator&#8217;s next() method doesn&#8217;t point to any object.<\/td><\/tr><tr><td><strong>SystemExit<\/strong><\/td><td>The sys.exit() function raises this error.<\/td><\/tr><tr><td><strong>StandardError<\/strong><\/td><td>Except for StopIteration and SystemExit, this is the base class for all built-in exceptions.<\/td><\/tr><tr><td><strong>ArithmeticError<\/strong><\/td><td>Base class for all errors that can happen when working with numbers.<\/td><\/tr><tr><td><strong>OverflowError<\/strong><\/td><td>Raised when a calculation uses more numbers than a numeric type can handle.<\/td><\/tr><tr><td><strong>FloatingPointError<\/strong><\/td><td>Raised when a calculation with a floating point number fails.<\/td><\/tr><tr><td><strong>ZeroDivisionError<\/strong><\/td><td>Raised when any type of number is divided by zero or moduloed by zero.<br>Raised when the second argument of a division or modulo operation is zero<\/td><\/tr><tr><td><strong>AssertionError<\/strong><\/td><td>If the Assert statement fails, this event is raised.<\/td><\/tr><tr><td><strong>AttributeError<\/strong><\/td><td>If the reference or assignment of an attribute fails, this event is raised.<\/td><\/tr><tr><td><strong>EOFError<\/strong><\/td><td>If the reference or assignment of an attribute fails, this event is raised.<\/td><\/tr><tr><td><strong>ImportError<\/strong><\/td><td>If an import statement fails, this event is thrown.<\/td><\/tr><tr><td><strong>KeyboardInterrupt<\/strong><\/td><td>When the user stops the program from running, usually by pressing Ctrl+c, this signal is sent.<\/td><\/tr><tr><td><strong>LookupError<\/strong><\/td><td>Base class for the exceptions that are thrown when a key or index used on a mapping or sequence is not valid.<\/td><\/tr><tr><td><strong>IndexError<\/strong><\/td><td>Thrown when a sequence can&#8217;t find an index.<\/td><\/tr><tr><td><strong>KeyError<\/strong><\/td><td>Raised when the key does not exist in the dictionary.<\/td><\/tr><tr><td><strong>NameError<\/strong><\/td><td>Raised when an identifier can&#8217;t be found in either the local or global namespace.<\/td><\/tr><tr><td><strong>UnboundLocalError<\/strong><\/td><td>Raised when a reference tries to use a local variable in a function or method that hasn&#8217;t been given a value yet.<\/td><\/tr><tr><td><strong>EnvironmentError<\/strong><\/td><td>This is the base class for all exceptions that happen outside of Python.<\/td><\/tr><tr><td><strong>IOError<\/strong><\/td><td>Raised when an input\/output operation fails, like when the print statement or the open() function try to open a file that doesn&#8217;t exist.<\/td><\/tr><tr><td><strong>SyntaxError<\/strong><\/td><td>Raised when there is a mistake in the way Python is written.<\/td><\/tr><tr><td><strong>IndentationError<\/strong><\/td><td>Raised when indentation is not specified properly.<\/td><\/tr><tr><td><strong>SystemError<\/strong><\/td><td>Raised when the interpreter finds an internal error. However, when this error is thrown, the Python interpreter does not close.<\/td><\/tr><tr><td><strong>SystemExit<\/strong><\/td><td>When the sys.exit() function is used to stop the Python interpreter, this event is raised. If the code doesn&#8217;t handle it, it makes the interpreter quit.<\/td><\/tr><tr><td><strong>TypeError<\/strong><\/td><td>Thrown when an operation or function is tried that doesn&#8217;t work with the data type that was given.<\/td><\/tr><tr><td><strong>ValueError<\/strong><\/td><td>An error occurs when a built-in operation or function receives an argument that has the right type but an invalid value.<\/td><\/tr><tr><td><strong>RuntimeError<\/strong><\/td><td>Raised when a mistake can&#8217;t be put into any of the other categories.<\/td><\/tr><tr><td><strong>NotImplementedError<\/strong><\/td><td>This error is shown when a class that has been inherited doesn&#8217;t implement an abstract method that it should.<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">List of Standard Exceptions in Python<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-summary\"><strong>Summary<\/strong><\/h2>\n\n\n\n<p>In summary, an exception is a Python object that represents an error. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>There can be multiple except blocks for a single try block. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>You can throw an exception by using the raise keyword in Python. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Consider the best practices in Python exception handling in doing your program.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Finally, if you missed any of our earlier lectures, you can always go to our <a href=\"https:\/\/itsourcecode.com\/topics\/python-tutorial\/\">Python Tutorial Topics<\/a> list.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>What is Exception Handling in Python? Exception handling in Python refers to the process of anticipating and handling errors or exceptional events that may occur during the execution of a &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Python Exception Handling Best Practices\" class=\"read-more button\" href=\"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/#more-73474\" aria-label=\"Read more about Python Exception Handling Best Practices\">Read more<\/a><\/p>\n","protected":false},"author":1373,"featured_media":73294,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61036],"tags":[93828,93825,93822,93823,93826,93821,93827,93824],"class_list":["post-73474","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-assertion-in-python","tag-finally-block-in-python","tag-python-built-in-exceptions","tag-python-exception-handling","tag-python-try-except-else","tag-python-try-except-else-finally","tag-try-else-in-python","tag-try-except-finally","resize-featured-image"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Exception Handling Best Practices<\/title>\n<meta name=\"description\" content=\"Discover the best practices for handling exceptions in Python. Learn how to effectively handle errors and exceptions in your Python code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Exception Handling Best Practices\" \/>\n<meta property=\"og:description\" content=\"Discover the best practices for handling exceptions in Python. Learn how to effectively handle errors and exceptions in your Python code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/\" \/>\n<meta property=\"og:site_name\" content=\"Itsourcecode.com\" \/>\n<meta property=\"article:author\" content=\"www.facebook.com\/unguardable7\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-18T07:13:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-23T04:21:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itsourcecode.com\/wp-content\/uploads\/2022\/07\/Python-Exceptions-Handling-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1460\" \/>\n\t<meta property=\"og:image:height\" content=\"900\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prince Ly Cesar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@http:\/\/www.twitter.com\/unguardable0729\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prince Ly Cesar\" \/>\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:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/\"},\"author\":{\"name\":\"Prince Ly Cesar\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/8d6ff1c108160ddbd60ff17cbb9f626b\"},\"headline\":\"Python Exception Handling Best Practices\",\"datePublished\":\"2023-07-18T07:13:55+00:00\",\"dateModified\":\"2023-11-23T04:21:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/\"},\"wordCount\":1097,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/ad9e0497e03b85a9ca299d935298f5dc\"},\"image\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/Python-Exceptions-Handling-1.png\",\"keywords\":[\"assertion in python\",\"finally block in python\",\"python built in exceptions\",\"python exception handling\",\"python try except else\",\"python try except else finally\",\"try else in python\",\"try except finally\"],\"articleSection\":[\"Python Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/\",\"name\":\"Python Exception Handling Best Practices\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/Python-Exceptions-Handling-1.png\",\"datePublished\":\"2023-07-18T07:13:55+00:00\",\"dateModified\":\"2023-11-23T04:21:59+00:00\",\"description\":\"Discover the best practices for handling exceptions in Python. Learn how to effectively handle errors and exceptions in your Python code.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/#primaryimage\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/Python-Exceptions-Handling-1.png\",\"contentUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/Python-Exceptions-Handling-1.png\",\"width\":1460,\"height\":900,\"caption\":\"Python Exception Handling - Try Except Else Finally\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/python-tutorial\\\/python-exception-handling-try-except-else-finally\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/itsourcecode.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Exception Handling Best Practices\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#website\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/\",\"name\":\"Itsourcecode.com\",\"description\":\"Partner In Your Coding Journey!\",\"publisher\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/ad9e0497e03b85a9ca299d935298f5dc\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/itsourcecode.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/ad9e0497e03b85a9ca299d935298f5dc\",\"name\":\"itsourcecode\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\",\"contentUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\",\"width\":409,\"height\":409,\"caption\":\"itsourcecode\"},\"logo\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\"},\"description\":\"Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!\",\"sameAs\":[\"https:\\\/\\\/itsourcecode.com\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/8d6ff1c108160ddbd60ff17cbb9f626b\",\"name\":\"Prince Ly Cesar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/litespeed\\\/avatar\\\/8fc9fa13530f18706fe0948f4673fae8.jpg?ver=1776478215\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/litespeed\\\/avatar\\\/8fc9fa13530f18706fe0948f4673fae8.jpg?ver=1776478215\",\"contentUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/litespeed\\\/avatar\\\/8fc9fa13530f18706fe0948f4673fae8.jpg?ver=1776478215\",\"caption\":\"Prince Ly Cesar\"},\"description\":\"Hi there! I'm Prince Ly Cesar a Student in Carlos Hilado Memorial State College - Binalbagan Campus and a Graphic Designer, Programmer &amp; Writer in IT SourceCode\",\"sameAs\":[\"https:\\\/\\\/activadorkeys.com\\\/\",\"www.facebook.com\\\/unguardable7\",\"http:\\\/\\\/www.instagram.com\\\/prnclycsr\\\/\",\"https:\\\/\\\/x.com\\\/http:\\\/\\\/www.twitter.com\\\/unguardable0729\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCF0BIqB5tpnCmwvYQCHucmw?view_as=subscriber\"],\"gender\":\"male\",\"knowsAbout\":[\"Graphic Designing\"],\"knowsLanguage\":[\"English\",\"Tagalog\"],\"jobTitle\":\"Graphic Designer\",\"worksFor\":\"IT SourceCode\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/author\\\/unguardable\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Exception Handling Best Practices","description":"Discover the best practices for handling exceptions in Python. Learn how to effectively handle errors and exceptions in your Python code.","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:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/","og_locale":"en_US","og_type":"article","og_title":"Python Exception Handling Best Practices","og_description":"Discover the best practices for handling exceptions in Python. Learn how to effectively handle errors and exceptions in your Python code.","og_url":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/","og_site_name":"Itsourcecode.com","article_author":"www.facebook.com\/unguardable7","article_published_time":"2023-07-18T07:13:55+00:00","article_modified_time":"2023-11-23T04:21:59+00:00","og_image":[{"width":1460,"height":900,"url":"https:\/\/itsourcecode.com\/wp-content\/uploads\/2022\/07\/Python-Exceptions-Handling-1.png","type":"image\/png"}],"author":"Prince Ly Cesar","twitter_card":"summary_large_image","twitter_creator":"@http:\/\/www.twitter.com\/unguardable0729","twitter_misc":{"Written by":"Prince Ly Cesar","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/#article","isPartOf":{"@id":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/"},"author":{"name":"Prince Ly Cesar","@id":"https:\/\/itsourcecode.com\/#\/schema\/person\/8d6ff1c108160ddbd60ff17cbb9f626b"},"headline":"Python Exception Handling Best Practices","datePublished":"2023-07-18T07:13:55+00:00","dateModified":"2023-11-23T04:21:59+00:00","mainEntityOfPage":{"@id":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/"},"wordCount":1097,"commentCount":0,"publisher":{"@id":"https:\/\/itsourcecode.com\/#\/schema\/person\/ad9e0497e03b85a9ca299d935298f5dc"},"image":{"@id":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/#primaryimage"},"thumbnailUrl":"https:\/\/itsourcecode.com\/wp-content\/uploads\/2022\/07\/Python-Exceptions-Handling-1.png","keywords":["assertion in python","finally block in python","python built in exceptions","python exception handling","python try except else","python try except else finally","try else in python","try except finally"],"articleSection":["Python Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/","url":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/","name":"Python Exception Handling Best Practices","isPartOf":{"@id":"https:\/\/itsourcecode.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/#primaryimage"},"image":{"@id":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/#primaryimage"},"thumbnailUrl":"https:\/\/itsourcecode.com\/wp-content\/uploads\/2022\/07\/Python-Exceptions-Handling-1.png","datePublished":"2023-07-18T07:13:55+00:00","dateModified":"2023-11-23T04:21:59+00:00","description":"Discover the best practices for handling exceptions in Python. Learn how to effectively handle errors and exceptions in your Python code.","breadcrumb":{"@id":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/#primaryimage","url":"https:\/\/itsourcecode.com\/wp-content\/uploads\/2022\/07\/Python-Exceptions-Handling-1.png","contentUrl":"https:\/\/itsourcecode.com\/wp-content\/uploads\/2022\/07\/Python-Exceptions-Handling-1.png","width":1460,"height":900,"caption":"Python Exception Handling - Try Except Else Finally"},{"@type":"BreadcrumbList","@id":"https:\/\/itsourcecode.com\/python-tutorial\/python-exception-handling-try-except-else-finally\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/itsourcecode.com\/"},{"@type":"ListItem","position":2,"name":"Python Exception Handling Best Practices"}]},{"@type":"WebSite","@id":"https:\/\/itsourcecode.com\/#website","url":"https:\/\/itsourcecode.com\/","name":"Itsourcecode.com","description":"Partner In Your Coding Journey!","publisher":{"@id":"https:\/\/itsourcecode.com\/#\/schema\/person\/ad9e0497e03b85a9ca299d935298f5dc"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itsourcecode.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/itsourcecode.com\/#\/schema\/person\/ad9e0497e03b85a9ca299d935298f5dc","name":"itsourcecode","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg","url":"https:\/\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg","contentUrl":"https:\/\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg","width":409,"height":409,"caption":"itsourcecode"},"logo":{"@id":"https:\/\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg"},"description":"Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!","sameAs":["https:\/\/itsourcecode.com\/"]},{"@type":"Person","@id":"https:\/\/itsourcecode.com\/#\/schema\/person\/8d6ff1c108160ddbd60ff17cbb9f626b","name":"Prince Ly Cesar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/itsourcecode.com\/wp-content\/litespeed\/avatar\/8fc9fa13530f18706fe0948f4673fae8.jpg?ver=1776478215","url":"https:\/\/itsourcecode.com\/wp-content\/litespeed\/avatar\/8fc9fa13530f18706fe0948f4673fae8.jpg?ver=1776478215","contentUrl":"https:\/\/itsourcecode.com\/wp-content\/litespeed\/avatar\/8fc9fa13530f18706fe0948f4673fae8.jpg?ver=1776478215","caption":"Prince Ly Cesar"},"description":"Hi there! I'm Prince Ly Cesar a Student in Carlos Hilado Memorial State College - Binalbagan Campus and a Graphic Designer, Programmer &amp; Writer in IT SourceCode","sameAs":["https:\/\/activadorkeys.com\/","www.facebook.com\/unguardable7","http:\/\/www.instagram.com\/prnclycsr\/","https:\/\/x.com\/http:\/\/www.twitter.com\/unguardable0729","https:\/\/www.youtube.com\/channel\/UCF0BIqB5tpnCmwvYQCHucmw?view_as=subscriber"],"gender":"male","knowsAbout":["Graphic Designing"],"knowsLanguage":["English","Tagalog"],"jobTitle":"Graphic Designer","worksFor":"IT SourceCode","url":"https:\/\/itsourcecode.com\/author\/unguardable\/"}]}},"_links":{"self":[{"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/posts\/73474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/users\/1373"}],"replies":[{"embeddable":true,"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/comments?post=73474"}],"version-history":[{"count":18,"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/posts\/73474\/revisions"}],"predecessor-version":[{"id":120651,"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/posts\/73474\/revisions\/120651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/media\/73294"}],"wp:attachment":[{"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/media?parent=73474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/categories?post=73474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsourcecode.com\/wp-json\/wp\/v2\/tags?post=73474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}