{"id":90,"date":"2026-03-01T17:50:27","date_gmt":"2026-03-01T12:50:27","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=90"},"modified":"2026-03-14T08:57:58","modified_gmt":"2026-03-14T03:57:58","slug":"raising-exceptions","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/raising-exceptions\/","title":{"rendered":"Raising Exceptions"},"content":{"rendered":"\n<p>In Python, you can manually trigger (raise) an error using the <code>raise<\/code> keyword.<\/p>\n\n\n\n<p>This is useful when you want to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stop program execution<\/li>\n\n\n\n<li>Enforce rules or validations<\/li>\n\n\n\n<li>Create custom error messages<\/li>\n\n\n\n<li>Prevent invalid data<\/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\">raise ExceptionType(\"Error message\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>SIMPLE EXAMPLE<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">age = -5if age &lt; 0:<br>    raise ValueError(\"Age cannot be negative.\")<\/pre>\n\n\n\n<p>If the condition is true, Python will stop execution and show the error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>RAISING EXCEPTION WITH USER INPUT<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">num = int(input(\"Enter a positive number: \"))if num &lt;= 0:<br>    raise ValueError(\"Number must be positive.\")print(\"You entered:\", num)<\/pre>\n\n\n\n<p>This ensures only valid input is accepted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>RAISING DIFFERENT TYPES OF EXCEPTIONS<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">name = \"\"if not name:<br>    raise TypeError(\"Name cannot be empty.\")<\/pre>\n\n\n\n<p>You can raise:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ValueError<\/code><\/li>\n\n\n\n<li><code>TypeError<\/code><\/li>\n\n\n\n<li><code>ZeroDivisionError<\/code><\/li>\n\n\n\n<li><code>FileNotFoundError<\/code><\/li>\n\n\n\n<li>Or other built-in exceptions<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>RAISE WITH TRY-EXCEPT<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    num = int(input(\"Enter number: \"))<br>    if num == 0:<br>        raise ZeroDivisionError(\"Zero is not allowed.\")<br>except ZeroDivisionError as e:<br>    print(\"Error:\", e)<\/pre>\n\n\n\n<p>Here, we manually raised an exception and handled it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>CREATING CUSTOM EXCEPTIONS<\/strong><\/h2>\n\n\n\n<p>You can create your own exception class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class InvalidAgeError(Exception):<br>    passage = -1if age &lt; 0:<br>    raise InvalidAgeError(\"Invalid age entered.\")<\/pre>\n\n\n\n<p>Custom exceptions make programs more professional and organized.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>WHY USE RAISE?<\/strong><\/h2>\n\n\n\n<p>\u2022 Validate user input<br>\u2022 Enforce business rules<br>\u2022 Stop incorrect program flow<br>\u2022 Improve debugging<br>\u2022 Build secure applications<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>KEY POINTS<\/strong><\/h2>\n\n\n\n<p>\u2022 Use <code>raise<\/code> to trigger an exception manually<br>\u2022 Provide clear error messages<br>\u2022 Can be used with built-in or custom exceptions<br>\u2022 Works together with <code>try<\/code> and <code>except<\/code><\/p>\n\n\n\n<p>Raising exceptions helps you control errors instead of waiting for them to happen automatically.<\/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 > Raising Exceptions<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773460817689\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":39,"template":"","class_list":["post-90","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>Raising Exceptions - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn how to use Python raise to trigger exceptions, validate input, enforce rules, and handle errors effectively.\" \/>\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\/raising-exceptions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Raising Exceptions - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn how to use Python raise to trigger exceptions, validate input, enforce rules, and handle errors effectively.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/raising-exceptions\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-14T03:57:58+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\\\/raising-exceptions\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/raising-exceptions\\\/\",\"name\":\"Raising Exceptions - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T12:50:27+00:00\",\"dateModified\":\"2026-03-14T03:57:58+00:00\",\"description\":\"Learn how to use Python raise to trigger exceptions, validate input, enforce rules, and handle errors effectively.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/raising-exceptions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/raising-exceptions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/raising-exceptions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > Exception Handling > Raising 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":"Raising Exceptions - One Language. Endless Possibilities","description":"Learn how to use Python raise to trigger exceptions, validate input, enforce rules, and handle errors effectively.","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\/raising-exceptions\/","og_locale":"en_US","og_type":"article","og_title":"Raising Exceptions - One Language. Endless Possibilities","og_description":"Learn how to use Python raise to trigger exceptions, validate input, enforce rules, and handle errors effectively.","og_url":"https:\/\/gigz.pk\/python\/lesson\/raising-exceptions\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-14T03:57:58+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\/raising-exceptions\/","url":"https:\/\/gigz.pk\/python\/lesson\/raising-exceptions\/","name":"Raising Exceptions - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T12:50:27+00:00","dateModified":"2026-03-14T03:57:58+00:00","description":"Learn how to use Python raise to trigger exceptions, validate input, enforce rules, and handle errors effectively.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/raising-exceptions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/raising-exceptions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/raising-exceptions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > Exception Handling > Raising 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\/90","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=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}