You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Directives on using try/except/finally blocks correctly and creating custom exceptions.
tags
python
error-handling
exceptions
best-practices
layer
Effective Python Error Handling
Primary Directive
You MUST use try/except/finally blocks to handle potential errors gracefully. Exceptions caught MUST be as specific as possible to avoid hiding bugs.
Process
Catch Specific Exceptions: Always catch the most specific exception possible (e.g., except ValueError:) rather than a bare except:. This prevents catching unexpected errors.
Use finally for Cleanup: Place code that MUST be executed regardless of whether an exception occurred (e.g., closing a file or a network connection) in a finally block.
Don't Suppress Exceptions: Do not use except: pass to silently ignore errors. If an exception is caught, it MUST be handled appropriately, logged, or explicitly re-raised.
Create Custom Exceptions: For application-specific error conditions, create custom exception classes by inheriting from Python's built-in Exception class. This makes error handling more explicit.
Keep try Blocks Small: The try block MUST contain only the specific line(s) of code that might raise the exception you are catching.
Constraints
Do NOT catch broad exceptions like Exception or BaseException unless you are at the top level of your application and intend to log the error before exiting.
Error messages in custom exceptions MUST be clear, informative, and describe the error condition precisely.