Exception Handling (try/except)
Exception Handling
Section titled “Exception Handling”Introduction
Section titled “Introduction”Exception handling allows your program to respond to errors gracefully instead of crashing. Python uses try/except blocks to catch and handle exceptions.
Exception Flow: try → except → else → finally
Section titled “Exception Flow: try → except → else → finally”flowchart TB Try["🔷 try:Code that might fail"] --> Exception{"❌ Exceptionraised?"}
Exception -->|"Yes"| Except["🔶 except:Handle the error"] Exception -->|"No"| Else["✅ else:Runs if NO exception(success path)"]
Except --> Finally["✅ finally:ALWAYS runs(cleanup: close files,release resources)"] Else --> Finally Try --> Finally
style Try fill:#4f46e5,color:#fff style Exception fill:#f59e0b,color:#000 style Except fill:#dc2626,color:#fff style Else fill:#059669,color:#fff style Finally fill:#7c3aed,color:#fffBasic Syntax
Section titled “Basic Syntax”try: # Code that might raise an exception risky_operation()except SomeException: # Code to handle the exception handle_error()try/except
Section titled “try/except”try: number = int(input("Enter a number: ")) print(f"100 / {number} = {100 / number}")except ValueError: print("That's not a valid number!")except ZeroDivisionError: print("Cannot divide by zero!")Catching Multiple Exceptions
Section titled “Catching Multiple Exceptions”try: value = int(input("Enter a number: ")) result = 100 / value print(result)except (ValueError, ZeroDivisionError) as e: print(f"Error: {e}")else and finally
Section titled “else and finally”try: file = open("data.txt", "r") content = file.read()except FileNotFoundError: print("File not found!")else: # Runs only if no exception occurred print(f"Read {len(content)} characters")finally: # ALWAYS runs — great for cleanup if 'file' in locals(): file.close() print("File closed")Raising Exceptions
Section titled “Raising Exceptions”def withdraw(balance, amount): if amount < 0: raise ValueError("Amount cannot be negative") if amount > balance: raise InsufficientFundsError("Insufficient balance") return balance - amountBest Practices
Section titled “Best Practices”- Be specific — catch specific exceptions, not bare
except: - Don’t swallow exceptions — unless you handle them meaningfully
- Use
finallyfor cleanup — close files, release resources - Prefer
else— keep success logic separate from error handling - Raise early, catch late — validate inputs at boundaries
Practice Exercises
Section titled “Practice Exercises”Exercise 1: Write a function that safely parses a list of strings into integers, returning only the valid ones.
Exercise 2: Create a simple bank account class that raises custom exceptions for invalid operations.