{"id":88,"date":"2026-03-01T17:42:50","date_gmt":"2026-03-01T12:42:50","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=88"},"modified":"2026-03-14T08:41:17","modified_gmt":"2026-03-14T03:41:17","slug":"multiple-exceptions","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/multiple-exceptions\/","title":{"rendered":"\u00a0Multiple Exceptions"},"content":{"rendered":"\n<p>Sometimes a single block of code can produce different types of errors.<br>Python allows you to handle multiple exceptions in different ways.<\/p>\n\n\n\n<p>Handling multiple exceptions makes your program more professional and stable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. USING MULTIPLE EXCEPT BLOCKS<\/strong><\/h2>\n\n\n\n<p>You can handle each error separately.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    num = int(input(\"Enter a number: \"))<br>    result = 10 \/ num<br>except ValueError:<br>    print(\"Invalid input. Please enter a valid number.\")<br>except ZeroDivisionError:<br>    print(\"You cannot divide by zero.\")<\/pre>\n\n\n\n<p>Each <code>except<\/code> block handles a specific error type.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. HANDLING MULTIPLE EXCEPTIONS IN ONE BLOCK<\/strong><\/h2>\n\n\n\n<p>You can handle multiple exceptions together using parentheses.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    num = int(input(\"Enter a number: \"))<br>    result = 10 \/ num<br>except (ValueError, ZeroDivisionError):<br>    print(\"Invalid input or division by zero occurred.\")<\/pre>\n\n\n\n<p>This is useful when you want the same message for different errors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. USING GENERIC EXCEPTION (AS e)<\/strong><\/h2>\n\n\n\n<p>If you want to catch all types of errors:<\/p>\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>Exception<\/code> is the base class for most errors.<br><code>e<\/code> stores the actual error message.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. ORDER OF EXCEPT BLOCKS<\/strong><\/h2>\n\n\n\n<p>Always write specific exceptions first, and generic exceptions last.<\/p>\n\n\n\n<p>Correct order:<\/p>\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.\")<br>except Exception as e:<br>    print(\"Other error:\", e)<\/pre>\n\n\n\n<p>If <code>Exception<\/code> is written first, specific errors will never execute.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>WHY HANDLE MULTIPLE EXCEPTIONS?<\/strong><\/h2>\n\n\n\n<p>\u2022 Different errors need different solutions<br>\u2022 Makes debugging easier<br>\u2022 Improves user experience<br>\u2022 Prevents unexpected crashes<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>KEY POINTS<\/strong><\/h2>\n\n\n\n<p>\u2022 Use multiple <code>except<\/code> blocks for different error types<br>\u2022 Use <code>(Error1, Error2)<\/code> to combine errors<br>\u2022 Use <code>Exception as e<\/code> for general handling<br>\u2022 Always place general exception at the end<\/p>\n\n\n\n<p>Handling multiple exceptions is an important step toward writing robust 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 > Multiple Exceptions<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773457930455\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p><\/p>\n","protected":false},"menu_order":37,"template":"","class_list":["post-88","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>\u00a0Multiple Exceptions - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn how to handle multiple exceptions in Python using try and except blocks to manage different errors safely and improve program stability.\" \/>\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\/multiple-exceptions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0Multiple Exceptions - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn how to handle multiple exceptions in Python using try and except blocks to manage different errors safely and improve program stability.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/multiple-exceptions\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-14T03:41:17+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\\\/multiple-exceptions\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/multiple-exceptions\\\/\",\"name\":\"\u00a0Multiple Exceptions - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T12:42:50+00:00\",\"dateModified\":\"2026-03-14T03:41:17+00:00\",\"description\":\"Learn how to handle multiple exceptions in Python using try and except blocks to manage different errors safely and improve program stability.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/multiple-exceptions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/multiple-exceptions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/multiple-exceptions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > Exception Handling > Multiple Exceptions\"}]},{\"@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":"\u00a0Multiple Exceptions - One Language. Endless Possibilities","description":"Learn how to handle multiple exceptions in Python using try and except blocks to manage different errors safely and improve program stability.","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\/multiple-exceptions\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0Multiple Exceptions - One Language. Endless Possibilities","og_description":"Learn how to handle multiple exceptions in Python using try and except blocks to manage different errors safely and improve program stability.","og_url":"https:\/\/gigz.pk\/python\/lesson\/multiple-exceptions\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-14T03:41:17+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\/multiple-exceptions\/","url":"https:\/\/gigz.pk\/python\/lesson\/multiple-exceptions\/","name":"\u00a0Multiple Exceptions - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T12:42:50+00:00","dateModified":"2026-03-14T03:41:17+00:00","description":"Learn how to handle multiple exceptions in Python using try and except blocks to manage different errors safely and improve program stability.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/multiple-exceptions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/multiple-exceptions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/multiple-exceptions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > Exception Handling > Multiple Exceptions"}]},{"@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\/88","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=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}