Exception handling should provide meaningful feedback and use appropriate exception types rather than failing silently or relying on fragile string-based checks. Avoid catching exceptions without proper logging, user feedback, or recovery mechanisms. Use built-in Python exceptions when available instead of creating custom ones unnecessarily.
Exception handling should provide meaningful feedback and use appropriate exception types rather than failing silently or relying on fragile string-based checks. Avoid catching exceptions without proper logging, user feedback, or recovery mechanisms. Use built-in Python exceptions when available instead of creating custom ones unnecessarily.
Key practices:
Example of problematic silent handling:
try:
result = subprocess.run(libreoffice_command, capture_output=True, text=True)
# ... process result
except Exception as _:
# Silent failure - no logging or user feedback
pass
Better approach with meaningful handling:
try:
result = subprocess.run(libreoffice_command, capture_output=True, text=True)
# ... process result
except subprocess.SubprocessError as e:
logger.warning(f"LibreOffice conversion failed: {e}")
return None
except FileNotFoundError: # Use built-in exception
raise FileNotFoundError(f"File {path} does not exist")
Enter the URL of a public GitHub repository