Day 7: Error and Exception Handling in Python – Build Resilient Code

Welcome to Day 7! Today, we’ll dive into error and exception handling, an essential aspect of programming that ensures your code doesn’t crash unexpectedly. Let’s explore how Python helps manage errors gracefully.

Table of Contents

What are Errors and Exceptions?

Errors: Issues in code that prevent execution (e.g., syntax errors).
Exceptions: Errors that occur during execution but can be handled (e.g., dividing by zero).

Common Errors in Python

Error Type Description Example
SyntaxError Incorrect Python syntax print("Hello (missing ))
TypeError Operation on incompatible types 5 + "string"
ValueError Function gets inappropriate input int("hello")
IndexError Accessing out-of-range list index lst[10] (list of size 5)
KeyError Key not found in dictionary dict['missing_key']
ZeroDivisionError Division by zero 10 / 0

What is Exception Handling?

Exception handling in Python uses the try-except block to catch and handle exceptions gracefully, avoiding program crashes.

The try-except Block

Syntax:

try:  
    # Code that might raise an exception  
except ExceptionType:  
    # Code to execute if an exception occurs  

Example:

try:  
    num = int(input("Enter a number: "))  
    print(10 / num)  
except ZeroDivisionError:  
    print("Cannot divide by zero!")  
except ValueError:  
    print("Invalid input. Please enter a number.")  

Catching Specific Exceptions

Handling multiple exceptions separately helps identify the issue precisely.

Example:

try:  
    lst = [1, 2, 3]  
    print(lst[5])  
except IndexError:  
    print("Index out of range!")  
except Exception as e:  
    print(f"An error occurred: {e}")  

Using else and finally

else Block: Executes if no exception is raised.
finally Block: Executes no matter what (cleanup code).

Example:

try:  
    file = open("data.txt", "r")  
    print(file.read())  
except FileNotFoundError:  
    print("File not found!")  
else:  
    print("File read successfully.")  
finally:  
    print("Closing the file.")  
    file.close()  

Raising Exceptions

You can raise exceptions intentionally using the raise keyword.

Example:

def check_age(age):  
    if age < 18:  
        raise ValueError("Age must be at least 18.")  
    return "Access granted."  

try:  
    print(check_age(15))  
except ValueError as e:  
    print(e)  

Custom Exceptions

Define custom exceptions by creating a class that inherits from Exception.

Example:

class NegativeNumberError(Exception):  
    pass  

def square_root(num):  
    if num < 0:  
        raise NegativeNumberError("Cannot calculate square root of a negative number.")  
    return num ** 0.5  

try:  
    print(square_root(-4))  
except NegativeNumberError as e:  
    print(e)  

Quick Practice

  1. Write a program that asks for two numbers and divides them, handling ZeroDivisionError and ValueError.
  2. Create a function that raises an exception if the input string is empty.
  3. Write a custom exception class for invalid email addresses and implement it

Wrap-Up

Today, you learned:

  • How to handle common Python errors and exceptions.
  • The importance of the try-except block for resilient code.
  • Advanced exception handling with else, finally, raising exceptions, and custom exceptions.
  • Practice exception handling to make your Python programs robust and user-friendly. Tomorrow, we’ll explore object-oriented programming (OOP) to bring structure and scalability to your code.

Happy coding! 🚀