{"id":485,"date":"2025-05-19T11:37:46","date_gmt":"2025-05-19T11:37:46","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=485"},"modified":"2025-05-20T12:56:49","modified_gmt":"2025-05-20T12:56:49","slug":"error-handling","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/python\/error-handling\/","title":{"rendered":"Error Handling"},"content":{"rendered":"<h2>Try, Except, Else, Finally<span style=\"font-weight: 400\"><br \/>\n<\/span><\/h2>\n<h3>What Is Exception Handling?<\/h3>\n<p>In Python, exceptions occur when an error disrupts the normal flow of a program (like dividing by zero or opening a non-existent file). Instead of crashing, you can catch and handle these errors using try and except.<\/p>\n<p><strong>Basic Syntax<\/strong><\/p>\n<p style=\"text-align: center\"><em>try:<\/em><br \/>\n<em># Code that might raise an exception<\/em><br \/>\n<em>except ExceptionType:<\/em><br \/>\n<em># Code that runs if an exception occurs<\/em><br \/>\n<em>else:<\/em><br \/>\n<em># Code that runs if no exception occurs<\/em><br \/>\n<em>finally:<\/em><br \/>\n<em># Code that always runs (no matter what)<\/em><\/p>\n<p><strong>try Block<\/strong><\/p>\n<p>The try block wraps code that might raise an error.<\/p>\n<p style=\"text-align: center\"><em>try:<\/em><br \/>\n<em>result = 10 \/ 0<\/em><\/p>\n<p><strong>except Block<\/strong><\/p>\n<p>Catches and handles the error. You can catch specific errors or use a generic except.<\/p>\n<p style=\"text-align: center\"><em>except ZeroDivisionError:<\/em><br \/>\n<em>print(&#8220;You can&#8217;t divide by zero.&#8221;)<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>try:<\/em><br \/>\n<em>num = int(input(&#8220;Enter a number: &#8220;))<\/em><br \/>\n<em>print(100 \/ num)<\/em><br \/>\n<em>except ValueError:<\/em><br \/>\n<em>print(&#8220;That&#8217;s not a number.&#8221;)<\/em><br \/>\n<em>except ZeroDivisionError:<\/em><br \/>\n<em>print(&#8220;Can&#8217;t divide by zero.&#8221;)<\/em><\/p>\n<p><strong>else Block<\/strong><\/p>\n<p>Runs only if no exception was raised in the try block.<\/p>\n<p style=\"text-align: center\"><em>try:<\/em><br \/>\n<em>result = 10 \/ 2<\/em><br \/>\n<em>except ZeroDivisionError:<\/em><br \/>\n<em>print(&#8220;Math error.&#8221;)<\/em><br \/>\n<em>else:<\/em><br \/>\n<em>print(&#8220;Division successful:&#8221;, result)<\/em><\/p>\n<p><strong>finally Block<\/strong><\/p>\n<p>Runs no matter what \u2014 whether an exception occurred or not. Great for cleanup code like closing files or releasing resources.<\/p>\n<p style=\"text-align: center\"><em>try:<\/em><br \/>\n<em>f = open(&#8220;data.txt&#8221;)<\/em><br \/>\n<em>data = f.read()<\/em><br \/>\n<em>except FileNotFoundError:<\/em><br \/>\n<em>print(&#8220;File not found.&#8221;)<\/em><br \/>\n<em>finally:<\/em><br \/>\n<em>print(&#8220;Done attempting file operation.&#8221;)<\/em><br \/>\n<em>if &#8216;f&#8217; in locals():<\/em><br \/>\n<em>f.close()<\/em><\/p>\n<p><strong>Catching Multiple Exceptions<\/strong><\/p>\n<p style=\"text-align: center\"><em>try:<\/em><br \/>\n<em># code<\/em><br \/>\n<em>except (TypeError, ValueError):<\/em><br \/>\n<em>print(&#8220;Type or Value error occurred.&#8221;)<\/em><\/p>\n<p><strong>Catching Any Exception (Not Always Recommended)<\/strong><\/p>\n<p style=\"text-align: center\"><em>try:<\/em><br \/>\n<em># risky code<\/em><br \/>\n<em>except Exception as e:<\/em><br \/>\n<em>print(&#8220;An error occurred:&#8221;, e)<\/em><\/p>\n<p>Use this only when you want to log or handle all errors generically \u2014 it can hide bugs if misused.<\/p>\n<p><strong>Summary:<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th>Block<\/th>\n<th>Purpose<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>try<\/code><\/td>\n<td>Code that might raise an exception<\/td>\n<\/tr>\n<tr>\n<td><code>except<\/code><\/td>\n<td>Handles specific exceptions (errors)<\/td>\n<\/tr>\n<tr>\n<td><code>else<\/code><\/td>\n<td>Runs only if no exceptions occurred<\/td>\n<\/tr>\n<tr>\n<td><code>finally<\/code><\/td>\n<td>Always executes (for cleanup actions)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Common exception types<\/h2>\n<p><strong>Common Exception Types in Python<\/strong><\/p>\n<h3>1. SyntaxError<\/h3>\n<ul>\n<li>Raised when the parser encounters a syntax mistake.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>if True print(&#8220;Hello&#8221;) # Missing colon<\/em><\/p>\n<h3>2. NameError<\/h3>\n<ul>\n<li>Raised when a variable or function name is not defined.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>print(age) # if `age` is not defined<\/em><\/p>\n<h3>3. TypeError<\/h3>\n<ul>\n<li>Raised when an operation or function is applied to the wrong data type.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>print(&#8220;Age: &#8221; + 25) # Mixing str and int<\/em><\/p>\n<h3>4. ValueError<\/h3>\n<ul>\n<li>Raised when a function gets the right type, but an inappropriate value.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>int(&#8220;abc&#8221;) # Can&#8217;t convert string to integer<\/em><\/p>\n<h3>5. IndexError<\/h3>\n<ul>\n<li>Raised when trying to access an invalid index in a list or string.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>numbers = [1, 2, 3]<\/em><br \/>\n<em>print(numbers[5])<\/em><\/p>\n<h3>6. KeyError<\/h3>\n<ul>\n<li>Raised when a key is not found in a dictionary.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>info = {&#8220;name&#8221;: &#8220;Alice&#8221;}<\/em><br \/>\n<em>print(info[&#8220;age&#8221;]) # &#8216;age&#8217; doesn&#8217;t exist<\/em><\/p>\n<h3>7. AttributeError<\/h3>\n<ul>\n<li>Raised when a variable doesn\u2019t have a referenced method or attribute.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>x = 10<\/em><br \/>\n<em>x.append(5) # int doesn&#8217;t have append()<\/em><\/p>\n<h3>8. ZeroDivisionError<\/h3>\n<ul>\n<li>Raised when dividing by zero.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>print(5 \/ 0)<\/em><\/p>\n<h3>9. ImportError \/ ModuleNotFoundError<\/h3>\n<ul>\n<li>Raised when an import fails or the module is missing.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>import non_existent_module<\/em><\/p>\n<h3>10. FileNotFoundError<\/h3>\n<ul>\n<li>Raised when a file operation fails due to a missing file.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>open(&#8220;missing_file.txt&#8221;)<\/em><\/p>\n<h3>11. IOError \/ OSError<\/h3>\n<ul>\n<li>General input\/output errors (file read\/write, permissions, etc.).<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>with open(&#8220;\/protected\/file.txt&#8221;, &#8220;r&#8221;) as f:<\/em><br \/>\n<em>&#8230;<\/em><\/p>\n<h3>12. IndentationError<\/h3>\n<ul>\n<li>Raised when there\u2019s a problem with indentation levels.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>def say_hi():<\/em><br \/>\n<em>print(&#8220;Hello&#8221;) # not indented correctly<\/em><\/p>\n<h3>13. RuntimeError<\/h3>\n<ul>\n<li>A generic error when no other exception type applies but something unexpected goes wrong.<\/li>\n<\/ul>\n<h3>14. StopIteration<\/h3>\n<ul>\n<li>Raised by iterators when there are no more items.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>it = iter([1, 2])<\/em><br \/>\n<em>next(it)<\/em><br \/>\n<em>next(it)<\/em><br \/>\n<em>next(it) # raises StopIteration<\/em><\/p>\n<h3>15. AssertionError<\/h3>\n<ul>\n<li>Raised when an assert statement fails.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>assert 2 + 2 == 5, &#8220;Math is broken!&#8221;<\/em><\/p>\n<p><strong>Tips for Handling<\/strong><\/p>\n<ul>\n<li>Catch specific exceptions to avoid hiding bugs.<\/li>\n<li>Use try-except-else-finally for clear, safe error handling.<\/li>\n<li>Use Exception class to handle unexpected errors only when needed.<\/li>\n<\/ul>\n<h2>Custom exceptions<\/h2>\n<h3>What Are Custom Exceptions?<\/h3>\n<p>Python allows you to create your own exception classes, giving you more control and clarity when things go wrong in your programs \u2014 especially in larger applications or libraries.<\/p>\n<p>Rather than relying solely on built-in exceptions like ValueError or TypeError, you can define custom exceptions tailored to your specific logic or business rules.<\/p>\n<p><strong>Defining a Custom Exception<\/strong><\/p>\n<p>To create one, define a new class that inherits from Exception or a subclass of it:<\/p>\n<p style=\"text-align: center\"><em>class MyCustomError(Exception):<\/em><br \/>\n<em>&#8220;&#8221;&#8221;A custom exception for specific application logic.&#8221;&#8221;&#8221;<\/em><br \/>\n<em>pass<\/em><\/p>\n<p><strong>Example:<\/strong> Using a Custom Exception<\/p>\n<p style=\"text-align: center\"><em>class NegativeAgeError(Exception):<\/em><br \/>\n<em>&#8220;&#8221;&#8221;Raised when a negative age is provided.&#8221;&#8221;&#8221;<\/em><br \/>\n<em>def __init__(self, age):<\/em><br \/>\n<em>super().__init__(f&#8221;Invalid age: {age}. Age cannot be negative.&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em>def set_age(age):<\/em><br \/>\n<em>if age &lt; 0:<\/em><br \/>\n<em>raise NegativeAgeError(age)<\/em><br \/>\n<em>print(f&#8221;Age set to {age}&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em>set_age(-5)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>Traceback (most recent call last):<\/em><br \/>\n<em>&#8230;<\/em><br \/>\n<em>NegativeAgeError: Invalid age: -5. Age cannot be negative.<\/em><\/p>\n<p><strong>Why Use Custom Exceptions?<\/strong><\/p>\n<ul>\n<li>More meaningful error messages.<\/li>\n<li>Easier to handle specific problems in try\/except blocks.<\/li>\n<li>Cleaner error management in larger or shared codebases.<\/li>\n<li>Enables domain-specific logic (e.g., InsufficientBalance, InvalidConfiguration, etc.).<\/li>\n<\/ul>\n<p><strong>Custom Exception Hierarchy (Optional)<\/strong><\/p>\n<p>You can create a base exception class for your application and then extend it:<\/p>\n<p style=\"text-align: center\"><em>class AppError(Exception):<\/em><br \/>\n<em>&#8220;&#8221;&#8221;Base class for all custom exceptions in the app.&#8221;&#8221;&#8221;<\/em><br \/>\n<em>pass<\/em><\/p>\n<p style=\"text-align: center\"><em>class InvalidInputError(AppError):<\/em><br \/>\n<em>pass<\/em><\/p>\n<p style=\"text-align: center\"><em>class DataNotFoundError(AppError):<\/em><br \/>\n<em>pass<\/em><\/p>\n<p>This way, you can catch all related errors at once:<\/p>\n<p style=\"text-align: center\"><em>try:<\/em><br \/>\n<em># risky operation<\/em><br \/>\n<em>raise InvalidInputError(&#8220;Bad data format&#8221;)<\/em><br \/>\n<em>except AppError as e:<\/em><br \/>\n<em>print(&#8220;Application error:&#8221;, e)<\/em><\/p>\n<p><strong>Best Practices<\/strong><\/p>\n<ul>\n<li>Inherit from Exception, not BaseException.<\/li>\n<li>Provide clear docstrings and custom messages.<\/li>\n<li>Use exception names that describe the problem (e.g., EmailFormatError).<\/li>\n<li>Consider organizing exceptions in a separate module for larger projects.<\/li>\n<\/ul>\n<p><strong>Summary:<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th><strong>Feature<\/strong><\/th>\n<th><strong>Description<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Purpose<\/td>\n<td>Handle application-specific errors with clear semantics<\/td>\n<\/tr>\n<tr>\n<td>How to define<\/td>\n<td><code>class CustomError(Exception): pass<\/code><br \/>\nor subclass another built-in exception<\/td>\n<\/tr>\n<tr>\n<td>Benefits<\/td>\n<td>Improved code clarity, better error categorization, and more precise error handling<\/td>\n<\/tr>\n<tr>\n<td>Example use cases<\/td>\n<td><code>NegativeAgeError<\/code>,<br \/>\n<code>InvalidEmailError<\/code>,<br \/>\n<code>PermissionDeniedError<\/code>,<br \/>\n<code>OutOfStockError<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Try, Except, Else, Finally What Is Exception Handling? In Python, exceptions occur when an error disrupts the normal flow of a program (like dividing by zero or opening a non-existent file). Instead of crashing, you can catch and handle these errors using try and except.<\/p>\n","protected":false},"author":1,"featured_media":486,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-485","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Error Handling - Python Course<\/title>\n<meta name=\"description\" content=\"Error handling in Python allows you to detect, manage, and respond to runtime issues gracefully using try, except, else, and finally blocks.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buhave.com\/courses\/python\/error-handling\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Error Handling - Python Course\" \/>\n<meta property=\"og:description\" content=\"Error handling in Python allows you to detect, manage, and respond to runtime issues gracefully using try, except, else, and finally blocks.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/python\/error-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"BUHAVE\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BeYouHave\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/naveedsafdarawan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T11:37:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-20T12:56:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Error-Handling.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Naveed Safdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Safdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"Error Handling\",\"datePublished\":\"2025-05-19T11:37:46+00:00\",\"dateModified\":\"2025-05-20T12:56:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/\"},\"wordCount\":901,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Error-Handling.webp\",\"articleSection\":[\"Python Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/\",\"name\":\"Error Handling - Python Course\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Error-Handling.webp\",\"datePublished\":\"2025-05-19T11:37:46+00:00\",\"dateModified\":\"2025-05-20T12:56:49+00:00\",\"description\":\"Error handling in Python allows you to detect, manage, and respond to runtime issues gracefully using try, except, else, and finally blocks.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Error-Handling.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Error-Handling.webp\",\"width\":1200,\"height\":628,\"caption\":\"Error Handling\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/error-handling\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Course\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/learn\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Error Handling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"name\":\"BUHAVE\",\"description\":\"Courses - Learn Online for Free\",\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/buhave.com\\\/courses\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\",\"name\":\"BUHAVE\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"width\":375,\"height\":75,\"caption\":\"BUHAVE\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/BeYouHave\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/buhave\",\"https:\\\/\\\/www.youtube.com\\\/@buhave\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\",\"name\":\"Naveed Safdar\",\"description\":\"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedsafdar\\\/\",\"https:\\\/\\\/www.facebook.com\\\/naveedsafdarawan\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@naveedsafdar\"],\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/author\\\/naveed-safdar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Error Handling - Python Course","description":"Error handling in Python allows you to detect, manage, and respond to runtime issues gracefully using try, except, else, and finally blocks.","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:\/\/buhave.com\/courses\/python\/error-handling\/","og_locale":"en_US","og_type":"article","og_title":"Error Handling - Python Course","og_description":"Error handling in Python allows you to detect, manage, and respond to runtime issues gracefully using try, except, else, and finally blocks.","og_url":"https:\/\/buhave.com\/courses\/python\/error-handling\/","og_site_name":"BUHAVE","article_publisher":"https:\/\/www.facebook.com\/BeYouHave\/","article_author":"https:\/\/www.facebook.com\/naveedsafdarawan\/","article_published_time":"2025-05-19T11:37:46+00:00","article_modified_time":"2025-05-20T12:56:49+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Error-Handling.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/python\/error-handling\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/python\/error-handling\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"Error Handling","datePublished":"2025-05-19T11:37:46+00:00","dateModified":"2025-05-20T12:56:49+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/error-handling\/"},"wordCount":901,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/error-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Error-Handling.webp","articleSection":["Python Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/python\/error-handling\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/python\/error-handling\/","url":"https:\/\/buhave.com\/courses\/python\/error-handling\/","name":"Error Handling - Python Course","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/error-handling\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/error-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Error-Handling.webp","datePublished":"2025-05-19T11:37:46+00:00","dateModified":"2025-05-20T12:56:49+00:00","description":"Error handling in Python allows you to detect, manage, and respond to runtime issues gracefully using try, except, else, and finally blocks.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/python\/error-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/python\/error-handling\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/python\/error-handling\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Error-Handling.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Error-Handling.webp","width":1200,"height":628,"caption":"Error Handling"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/python\/error-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Courses","item":"https:\/\/buhave.com\/courses\/"},{"@type":"ListItem","position":2,"name":"Python Course","item":"https:\/\/buhave.com\/courses\/learn\/python\/"},{"@type":"ListItem","position":3,"name":"Error Handling"}]},{"@type":"WebSite","@id":"https:\/\/buhave.com\/courses\/#website","url":"https:\/\/buhave.com\/courses\/","name":"BUHAVE","description":"Courses - Learn Online for Free","publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buhave.com\/courses\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/buhave.com\/courses\/#organization","name":"BUHAVE","url":"https:\/\/buhave.com\/courses\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","width":375,"height":75,"caption":"BUHAVE"},"image":{"@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/BeYouHave\/","https:\/\/www.linkedin.com\/company\/buhave","https:\/\/www.youtube.com\/@buhave"]},{"@type":"Person","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca","name":"Naveed Safdar","description":"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.","sameAs":["https:\/\/www.linkedin.com\/in\/naveedsafdar\/","https:\/\/www.facebook.com\/naveedsafdarawan\/","https:\/\/www.youtube.com\/@naveedsafdar"],"url":"https:\/\/buhave.com\/courses\/author\/naveed-safdar\/"}]}},"_links":{"self":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/comments?post=485"}],"version-history":[{"count":2,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/485\/revisions"}],"predecessor-version":[{"id":766,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/485\/revisions\/766"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/486"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}