This directory contains comprehensive resources for mastering error handling in Python, from basic exception handling to advanced error recovery strategies.
Error handling is crucial for building robust, maintainable Python applications. This collection covers everything from fundamental try-except blocks to sophisticated error recovery patterns used in production systems.
Start with these fundamentals:
- Exception basics - Understanding how exceptions work
- Try-except patterns - Basic error handling structures
- Common exceptions - Built-in exception types and when they occur
Build on the basics:
- Custom exceptions - Creating meaningful error types
- Exception chaining - Preserving error context
- Error logging - Comprehensive logging strategies
Master professional techniques:
- Context managers - Resource cleanup and error handling
- Defensive programming - Writing error-resistant code
- Error recovery - Sophisticated recovery strategies
| File | Topic | Difficulty | Description |
|---|---|---|---|
01-exception-basics.py |
Exception Fundamentals | Beginner | Core concepts, try-except blocks, exception hierarchy |
02-try-except-patterns.py |
Try-Except Patterns | Beginner-Intermediate | Advanced patterns, multiple exceptions, else/finally |
03-custom-exceptions.py |
Custom Exceptions | Intermediate | Creating custom exception classes and hierarchies |
04-exception-chaining.py |
Exception Chaining | Intermediate | Preserving error context with raise from |
05-logging-errors.py |
Error Logging | Intermediate | Comprehensive logging strategies and best practices |
06-context-managers-cleanup.py |
Context Managers | Advanced | Resource management and cleanup with context managers |
07-defensive-programming.py |
Defensive Programming | Advanced | Writing robust, error-resistant code |
08-error-recovery-strategies.py |
Error Recovery | Advanced | Retry mechanisms, circuit breakers, graceful degradation |
For a comprehensive learning experience, begin with the detailed tutorial:
# Read the complete tutorial guide
cat tutorial/README.mdEach file contains runnable examples with detailed explanations:
# Example: Basic exception handling
python 01-exception-basics.py
# Example: Advanced error recovery
python 08-error-recovery-strategies.pyEach file includes practical exercises and real-world scenarios to help you apply the concepts.
- Exception hierarchy - Understanding Python's exception structure
- Try-except-else-finally - Complete exception handling flow
- Exception objects - Working with exception instances
- Stack traces - Reading and preserving error information
- Custom exception hierarchies - Designing meaningful error types
- Exception chaining - Preserving original error context
- Context managers - Automatic resource cleanup
- Error recovery strategies - Retry, circuit breaker, fallback patterns
- Logging strategies - Structured error logging
- Monitoring and alerting - Error tracking in production
- Performance considerations - Efficient exception handling
- Testing exceptions - Unit testing error scenarios
# Robust file handling with proper cleanup
try:
with open('data.txt', 'r') as file:
data = file.read()
process_data(data)
except FileNotFoundError:
logger.error("Data file not found")
use_default_data()
except PermissionError:
logger.error("Permission denied accessing file")
request_elevated_permissions()# Network requests with retry logic
@retry(max_attempts=3, backoff_factor=2)
def fetch_data(url):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
logger.warning(f"Request failed: {e}")
raise# Database operations with transaction handling
class DatabaseManager:
def safe_transaction(self, operations):
try:
self.begin_transaction()
for operation in operations:
operation()
self.commit()
except DatabaseError:
self.rollback()
raise
finally:
self.close_connection()- Be specific - Catch specific exception types, not generic
Exception - Preserve context - Use exception chaining with
raise from - Log appropriately - Include relevant context in error messages
- Clean up resources - Use context managers or try-finally blocks
- Fail fast - Validate inputs early and raise meaningful errors
- Document exceptions - Specify what exceptions your functions can raise
- Don't ignore exceptions - Avoid bare
except:clauses - Don't catch and continue silently - Always handle or re-raise
- Don't use exceptions for control flow - Exceptions should be exceptional
- Don't expose internal details - Sanitize error messages for users
- Don't forget cleanup - Always release resources properly
"Easier to Ask for Forgiveness than Permission" - Python's preferred approach:
# EAFP (Pythonic)
try:
value = my_dict[key]
except KeyError:
value = default_value
# LBYL (Less Pythonic)
if key in my_dict:
value = my_dict[key]
else:
value = default_valueCreate clear error boundaries in your application:
class APIErrorBoundary:
def __call__(self, func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except ValidationError as e:
return {"error": "Invalid input", "details": str(e)}
except DatabaseError as e:
logger.error(f"Database error: {e}")
return {"error": "Internal server error"}
except Exception as e:
logger.critical(f"Unexpected error: {e}")
return {"error": "Service unavailable"}
return wrapperAfter studying the materials, test your understanding:
- Basic Level: Can you handle file operations safely?
- Intermediate Level: Can you create custom exception hierarchies?
- Advanced Level: Can you implement retry mechanisms and circuit breakers?
- Logging -
../logging/(if available) - Testing -
../../testing/for testing exception scenarios - Async Programming -
../async-programming/for async exception handling
- Python Exception Handling Documentation
- PEP 3134 - Exception Chaining
- Effective Python - Exception Handling
When adding new error handling examples:
- Follow the existing file naming convention
- Include comprehensive docstrings and comments
- Provide both basic and advanced examples
- Add practical, real-world scenarios
- Update this README with new content
Next Steps: Start with 01-exception-basics.py or dive into the comprehensive tutorial/README.md for a complete learning experience!