{"id":68,"date":"2025-10-21T04:29:19","date_gmt":"2025-10-21T04:29:19","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=68"},"modified":"2025-12-17T07:47:27","modified_gmt":"2025-12-17T07:47:27","slug":"lesson-12-exception-handling-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=68","title":{"rendered":"Lesson 12: Exception Handling in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand how to <strong>handle errors<\/strong> in Python gracefully using <strong>exceptions<\/strong>, preventing program crashes and improving reliability.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. What Is an Exception?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An <strong>exception<\/strong> is an <strong>error that occurs during program execution<\/strong>.<br>Examples: dividing by zero, file not found, invalid input, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>num = 10\nprint(num \/ 0)  # This will raise ZeroDivisionError\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZeroDivisionError: division by zero\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Try and Except Block<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>try<\/code> to test code and <code>except<\/code> to handle exceptions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    num = int(input(\"Enter a number: \"))\n    print(10 \/ num)\nexcept ZeroDivisionError:\n    print(\"Cannot divide by zero!\")\nexcept ValueError:\n    print(\"Invalid input! Enter an integer.\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Behavior:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If input is 0 \u2192 prints &#8220;Cannot divide by zero!&#8221;<\/li>\n\n\n\n<li>If input is not a number \u2192 prints &#8220;Invalid input! Enter an integer.&#8221;<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>3. Handling Multiple Exceptions<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can handle multiple exceptions in one block.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    x = int(input(\"Enter a number: \"))\n    y = int(input(\"Enter another number: \"))\n    result = x \/ y\nexcept (ZeroDivisionError, ValueError) as e:\n    print(\"Error occurred:\", e)\nelse:\n    print(\"Result is:\", result)\nfinally:\n    print(\"Execution finished.\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf0 <strong>4. Common Built-in Exceptions<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Exception<\/th><th>Cause<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><code>ZeroDivisionError<\/code><\/td><td>Division by zero<\/td><td><code>10\/0<\/code><\/td><\/tr><tr><td><code>ValueError<\/code><\/td><td>Invalid value<\/td><td><code>int(\"abc\")<\/code><\/td><\/tr><tr><td><code>FileNotFoundError<\/code><\/td><td>File doesn\u2019t exist<\/td><td><code>open(\"nofile.txt\")<\/code><\/td><\/tr><tr><td><code>TypeError<\/code><\/td><td>Wrong data type<\/td><td><code>\"5\" + 5<\/code><\/td><\/tr><tr><td><code>IndexError<\/code><\/td><td>Invalid index<\/td><td><code>arr[10]<\/code><\/td><\/tr><tr><td><code>KeyError<\/code><\/td><td>Key not found in dict<\/td><td><code>dict[\"x\"]<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd01 <strong>5. Try, Except, Else, Finally<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    num = int(input(\"Enter a number: \"))\n    result = 10 \/ num\nexcept ZeroDivisionError:\n    print(\"Cannot divide by zero!\")\nelse:\n    print(\"Division successful, result:\", result)\nfinally:\n    print(\"End of program\")\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>try<\/code>: code that may raise an exception<\/li>\n\n\n\n<li><code>except<\/code>: code to handle exception<\/li>\n\n\n\n<li><code>else<\/code>: runs if no exception occurs<\/li>\n\n\n\n<li><code>finally<\/code>: always runs<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>6. Raising Exceptions<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can raise exceptions intentionally using <code>raise<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>age = int(input(\"Enter your age: \"))\nif age &lt; 18:\n    raise ValueError(\"Age must be 18 or above.\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf0d <strong>7. Real-Life Example \u2013 Safe Input<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while True:\n    try:\n        num = int(input(\"Enter a positive number: \"))\n        if num &lt; 0:\n            raise ValueError(\"Number cannot be negative\")\n        break\n    except ValueError as e:\n        print(\"Error:\", e)\n\nprint(\"You entered:\", num)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand how to handle errors in Python gracefully using exceptions, preventing program crashes and improving reliability. \ud83e\udde9 1. What Is an Exception? An exception is an error that occurs during program execution.Examples: dividing by zero, file not found, invalid input, etc. \u2705 Example: Output: \u2699\ufe0f 2. Try and Except Block Use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}