{"id":87,"date":"2026-03-01T17:38:07","date_gmt":"2026-03-01T12:38:07","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=87"},"modified":"2026-03-14T08:06:47","modified_gmt":"2026-03-14T03:06:47","slug":"try-and-except","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/try-and-except\/","title":{"rendered":"Try and Except"},"content":{"rendered":"\n<p><code>try<\/code> and <code>except<\/code> are used for error handling in Python.<br>They prevent your program from crashing when an error occurs.<\/p>\n\n\n\n<p>Instead of stopping the program, Python executes the <code>except<\/code> block.<\/p>\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>    # code that may cause error<br>except:<br>    # code that runs if error occurs<\/pre>\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>    num = int(input(\"Enter a number: \"))<br>    print(10 \/ num)<br>except:<br>    print(\"An error occurred.\")<\/pre>\n\n\n\n<p>If the user enters <code>0<\/code> or text, the program will not crash.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>HANDLING SPECIFIC ERRORS<\/strong><\/h2>\n\n\n\n<p>It is better to handle specific exceptions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. ZeroDivisionError<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    print(10 \/ 0)<br>except ZeroDivisionError:<br>    print(\"You cannot divide by zero.\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. ValueError<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    num = int(\"abc\")<br>except ValueError:<br>    print(\"Invalid input. Please enter a number.\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>MULTIPLE EXCEPT BLOCKS<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    num = int(input(\"Enter number: \"))<br>    result = 10 \/ num<br>except ValueError:<br>    print(\"Invalid number.\")<br>except ZeroDivisionError:<br>    print(\"Cannot divide by zero.\")<\/pre>\n\n\n\n<p>Each error type is handled separately.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>USING AS e (SHOW ERROR MESSAGE)<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    num = int(\"abc\")<br>except Exception as e:<br>    print(\"Error:\", e)<\/pre>\n\n\n\n<p><code>e<\/code> stores the actual error message.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TRY \u2013 EXCEPT \u2013 ELSE<\/strong><\/h2>\n\n\n\n<p>The <code>else<\/code> block runs if no error occurs.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    num = int(input(\"Enter number: \"))<br>except ValueError:<br>    print(\"Invalid input.\")<br>else:<br>    print(\"You entered:\", num)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TRY \u2013 EXCEPT \u2013 FINALLY<\/strong><\/h2>\n\n\n\n<p>The <code>finally<\/code> block runs no matter what.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    print(10 \/ 2)<br>except ZeroDivisionError:<br>    print(\"Error\")<br>finally:<br>    print(\"Execution completed.\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>COMPLETE STRUCTURE<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    # risky code<br>except SpecificError:<br>    # handle specific error<br>except Exception as e:<br>    # handle other errors<br>else:<br>    # runs if no error<br>finally:<br>    # always runs<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>WHY USE TRY &amp; EXCEPT?<\/strong><\/h2>\n\n\n\n<p>\u2022 Prevents program crashes<br>\u2022 Makes programs user-friendly<br>\u2022 Handles unexpected situations<br>\u2022 Important for file handling, databases, APIs, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>KEY TAKEAWAYS<\/strong><\/h2>\n\n\n\n<p>\u2022 <code>try<\/code> contains risky code<br>\u2022 <code>except<\/code> handles errors<br>\u2022 <code>else<\/code> runs if no error<br>\u2022 <code>finally<\/code> always runs<br>\u2022 Handle specific errors when possible<\/p>\n\n\n\n<p><code>try<\/code> and <code>except<\/code> are essential for writing safe and professional Python programs.<\/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 > Try and Except<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773457649803\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":36,"template":"","class_list":["post-87","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>Try and Except - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python try and except for error handling. Prevent crashes, manage exceptions, and write safe programs with examples.\" \/>\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\/try-and-except\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Try and Except - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python try and except for error handling. Prevent crashes, manage exceptions, and write safe programs with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/try-and-except\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-14T03:06:47+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\\\/try-and-except\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/try-and-except\\\/\",\"name\":\"Try and Except - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T12:38:07+00:00\",\"dateModified\":\"2026-03-14T03:06:47+00:00\",\"description\":\"Learn Python try and except for error handling. Prevent crashes, manage exceptions, and write safe programs with examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/try-and-except\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/try-and-except\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/try-and-except\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > Exception Handling > Try and Except\"}]},{\"@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":"Try and Except - One Language. Endless Possibilities","description":"Learn Python try and except for error handling. Prevent crashes, manage exceptions, and write safe programs with examples.","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\/try-and-except\/","og_locale":"en_US","og_type":"article","og_title":"Try and Except - One Language. Endless Possibilities","og_description":"Learn Python try and except for error handling. Prevent crashes, manage exceptions, and write safe programs with examples.","og_url":"https:\/\/gigz.pk\/python\/lesson\/try-and-except\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-14T03:06:47+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\/try-and-except\/","url":"https:\/\/gigz.pk\/python\/lesson\/try-and-except\/","name":"Try and Except - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T12:38:07+00:00","dateModified":"2026-03-14T03:06:47+00:00","description":"Learn Python try and except for error handling. Prevent crashes, manage exceptions, and write safe programs with examples.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/try-and-except\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/try-and-except\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/try-and-except\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > Exception Handling > Try and Except"}]},{"@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\/87","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=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}