{"id":91,"date":"2026-03-02T07:10:29","date_gmt":"2026-03-02T02:10:29","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=91"},"modified":"2026-03-14T09:03:32","modified_gmt":"2026-03-14T04:03:32","slug":"custom-exceptions","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/custom-exceptions\/","title":{"rendered":"Custom Exceptions"},"content":{"rendered":"\n<p>Custom exceptions allow you to create your own error types.<br>They are useful when built-in exceptions do not clearly describe your program\u2019s specific error.<\/p>\n\n\n\n<p>Custom exceptions make your code more readable, professional, and organized.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>WHY CREATE CUSTOM EXCEPTIONS?<\/strong><\/h2>\n\n\n\n<p>\u2022 Enforce business rules<br>\u2022 Improve error clarity<br>\u2022 Make debugging easier<br>\u2022 Handle application-specific problems<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>CREATING A CUSTOM EXCEPTION<\/strong><\/h2>\n\n\n\n<p>To create a custom exception, define a class that inherits from <code>Exception<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class CustomError(Exception):<br>    pass<\/pre>\n\n\n\n<p>Now you can use this exception in your program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>SIMPLE EXAMPLE<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">class InvalidAgeError(Exception):<br>    passage = int(input(\"Enter your age: \"))if age &lt; 0:<br>    raise InvalidAgeError(\"Age cannot be negative.\")print(\"Age accepted.\")<\/pre>\n\n\n\n<p>If the user enters a negative age, the custom exception will be raised.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>USING CUSTOM EXCEPTION WITH TRY-EXCEPT<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">class InvalidMarksError(Exception):<br>    passtry:<br>    marks = int(input(\"Enter marks: \"))<br>    if marks &lt; 0 or marks &gt; 100:<br>        raise InvalidMarksError(\"Marks must be between 0 and 100.\")<br>except InvalidMarksError as e:<br>    print(\"Error:\", e)<\/pre>\n\n\n\n<p>Here, the custom exception is raised and handled properly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>ADDING CUSTOM MESSAGE WITH INIT<\/strong><\/h2>\n\n\n\n<p>You can customize the exception class further.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class NegativeNumberError(Exception):<br>    def __init__(self, message):<br>        self.message = message<br>        super().__init__(self.message)try:<br>    num = int(input(\"Enter number: \"))<br>    if num &lt; 0:<br>        raise NegativeNumberError(\"Negative numbers are not allowed.\")<br>except NegativeNumberError as e:<br>    print(\"Error:\", e)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>REAL-WORLD USE CASE<\/strong><\/h2>\n\n\n\n<p>Example: Bank account balance validation<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class InsufficientBalanceError(Exception):<br>    passbalance = 500<br>withdraw = 1000if withdraw &gt; balance:<br>    raise InsufficientBalanceError(\"Not enough balance.\")<\/pre>\n\n\n\n<p>This makes your application logic clearer and more structured.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>BEST PRACTICES<\/strong><\/h2>\n\n\n\n<p>\u2022 Inherit from <code>Exception<\/code><br>\u2022 Use meaningful class names<br>\u2022 Provide clear error messages<br>\u2022 Use custom exceptions for specific business logic<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>KEY TAKEAWAY<\/strong><\/h2>\n\n\n\n<p>Custom exceptions allow you to define and handle application-specific errors clearly and professionally, making your Python programs stronger and easier to maintain.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773461134256\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\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 > Custom Exceptions<\/span><\/span><\/div>","protected":false},"menu_order":40,"template":"","class_list":["post-91","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>Custom Exceptions - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn how to create and handle Python custom exceptions to manage application-specific errors and improve code clarity professionally.\" \/>\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\/custom-exceptions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom Exceptions - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn how to create and handle Python custom exceptions to manage application-specific errors and improve code clarity professionally.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/custom-exceptions\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-14T04:03:32+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\\\/custom-exceptions\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/custom-exceptions\\\/\",\"name\":\"Custom Exceptions - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T02:10:29+00:00\",\"dateModified\":\"2026-03-14T04:03:32+00:00\",\"description\":\"Learn how to create and handle Python custom exceptions to manage application-specific errors and improve code clarity professionally.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/custom-exceptions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/custom-exceptions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/custom-exceptions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > Exception Handling > Custom 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":"Custom Exceptions - One Language. Endless Possibilities","description":"Learn how to create and handle Python custom exceptions to manage application-specific errors and improve code clarity professionally.","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\/custom-exceptions\/","og_locale":"en_US","og_type":"article","og_title":"Custom Exceptions - One Language. Endless Possibilities","og_description":"Learn how to create and handle Python custom exceptions to manage application-specific errors and improve code clarity professionally.","og_url":"https:\/\/gigz.pk\/python\/lesson\/custom-exceptions\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-14T04:03:32+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\/custom-exceptions\/","url":"https:\/\/gigz.pk\/python\/lesson\/custom-exceptions\/","name":"Custom Exceptions - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T02:10:29+00:00","dateModified":"2026-03-14T04:03:32+00:00","description":"Learn how to create and handle Python custom exceptions to manage application-specific errors and improve code clarity professionally.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/custom-exceptions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/custom-exceptions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/custom-exceptions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > Exception Handling > Custom 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\/91","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=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}