{"id":89,"date":"2026-03-01T17:46:16","date_gmt":"2026-03-01T12:46:16","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=89"},"modified":"2026-03-14T08:51:57","modified_gmt":"2026-03-14T03:51:57","slug":"finally-block","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/finally-block\/","title":{"rendered":"Finally Block"},"content":{"rendered":"\n<p>The <code>finally<\/code> block is used in exception handling.<br>It always executes \u2014 whether an error occurs or not.<\/p>\n\n\n\n<p>It is mainly used for <strong>cleanup tasks<\/strong> such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Closing files<\/li>\n\n\n\n<li>Releasing resources<\/li>\n\n\n\n<li>Closing database connections<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>BASIC SYNTAX<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    # risky code<br>except:<br>    # handle error<br>finally:<br>    # always runs<\/pre>\n\n\n\n<p>The <code>finally<\/code> block runs no matter what happens in the <code>try<\/code> block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>SIMPLE EXAMPLE<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    print(10 \/ 2)<br>except ZeroDivisionError:<br>    print(\"Cannot divide by zero.\")<br>finally:<br>    print(\"Execution completed.\")<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5.0<br>Execution completed.<\/pre>\n\n\n\n<p>Even though no error occurred, <code>finally<\/code> still executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>EXAMPLE WITH ERROR<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    print(10 \/ 0)<br>except ZeroDivisionError:<br>    print(\"Cannot divide by zero.\")<br>finally:<br>    print(\"Execution completed.\")<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Cannot divide by zero.<br>Execution completed.<\/pre>\n\n\n\n<p>The <code>finally<\/code> block runs even when an error occurs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FINALLY WITH FILE HANDLING<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    file = open(\"data.txt\", \"r\")<br>    content = file.read()<br>except FileNotFoundError:<br>    print(\"File not found.\")<br>finally:<br>    print(\"File operation finished.\")<\/pre>\n\n\n\n<p>Even if the file does not exist, the <code>finally<\/code> block runs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TRY \u2013 EXCEPT \u2013 ELSE \u2013 FINALLY STRUCTURE<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    num = int(input(\"Enter a number: \"))<br>except ValueError:<br>    print(\"Invalid input.\")<br>else:<br>    print(\"You entered:\", num)<br>finally:<br>    print(\"Program ended.\")<\/pre>\n\n\n\n<p>Execution order:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>try<\/code> runs first<\/li>\n\n\n\n<li>If error \u2192 <code>except<\/code> runs<\/li>\n\n\n\n<li>If no error \u2192 <code>else<\/code> runs<\/li>\n\n\n\n<li><code>finally<\/code> always runs<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>IMPORTANT POINTS<\/strong><\/h2>\n\n\n\n<p>\u2022 <code>finally<\/code> always executes<br>\u2022 Used for cleanup tasks<br>\u2022 Works with or without <code>except<\/code><br>\u2022 Commonly used in file and database operations<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>KEY TAKEAWAY<\/strong><\/h2>\n\n\n\n<p>The <code>finally<\/code> block ensures important code runs no matter what happens, making your Python programs safer and more reliable.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON INTERMEDIATE (PYI) > Exception Handling > Finally Block<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773459954352\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":38,"template":"","class_list":["post-89","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Finally Block - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn how the Python finally block works in exception handling with syntax and examples to ensure cleanup code always executes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/finally-block\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Finally Block - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn how the Python finally block works in exception handling with syntax and examples to ensure cleanup code always executes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/finally-block\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-14T03:51:57+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/finally-block\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/finally-block\\\/\",\"name\":\"Finally Block - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T12:46:16+00:00\",\"dateModified\":\"2026-03-14T03:51:57+00:00\",\"description\":\"Learn how the Python finally block works in exception handling with syntax and examples to ensure cleanup code always executes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/finally-block\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/finally-block\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/finally-block\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > Exception Handling > Finally Block\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Finally Block - One Language. Endless Possibilities","description":"Learn how the Python finally block works in exception handling with syntax and examples to ensure cleanup code always executes.","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:\/\/gigz.pk\/python\/lesson\/finally-block\/","og_locale":"en_US","og_type":"article","og_title":"Finally Block - One Language. Endless Possibilities","og_description":"Learn how the Python finally block works in exception handling with syntax and examples to ensure cleanup code always executes.","og_url":"https:\/\/gigz.pk\/python\/lesson\/finally-block\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-14T03:51:57+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/finally-block\/","url":"https:\/\/gigz.pk\/python\/lesson\/finally-block\/","name":"Finally Block - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T12:46:16+00:00","dateModified":"2026-03-14T03:51:57+00:00","description":"Learn how the Python finally block works in exception handling with syntax and examples to ensure cleanup code always executes.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/finally-block\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/finally-block\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/finally-block\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > Exception Handling > Finally Block"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}