Always provide clear feedback when operations fail, even when continuing with fallbacks or alternative approaches. Silent failures make debugging extremely difficult and can lead to unexpected behavior in production systems.
Key practices:
Example of proper optional dependency handling:
# Good - defer error until usage
try:
import optional_library
except ImportError:
optional_library = None
def use_optional_feature():
if optional_library is None:
raise ImportError("optional_library is required for this feature. Install with: pip install optional_library")
return optional_library.do_something()
# Bad - fails at import time
import optional_library # This breaks if not installed
Example of proper fallback with feedback:
# Good - inform about fallback
try:
result = primary_operation()
except SpecificError as e:
logger.warning(f"Primary operation failed: {e}. Falling back to alternative.")
result = fallback_operation()
# Bad - silent fallback
try:
result = primary_operation()
except:
result = fallback_operation() # No indication why fallback was used
This approach ensures that failures are visible to developers and operators, making systems more maintainable and debuggable.
Enter the URL of a public GitHub repository