mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Add more details to E722 (bare-except) docs (#5007)
## Summary Note that catching a bare `Exception` is better than catching no specific exception. ## Test Plan Documentation only.
This commit is contained in:
parent
445e1723ab
commit
9f7cc86a22
1 changed files with 12 additions and 1 deletions
|
@ -12,7 +12,7 @@ use ruff_python_ast::source_code::Locator;
|
|||
/// A bare `except` catches `BaseException` which includes
|
||||
/// `KeyboardInterrupt`, `SystemExit`, `Exception`, and others. Catching
|
||||
/// `BaseException` can make it hard to interrupt the program (e.g., with
|
||||
/// Ctrl-C) and disguise other problems.
|
||||
/// Ctrl-C) and can disguise other problems.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
|
@ -30,6 +30,17 @@ use ruff_python_ast::source_code::Locator;
|
|||
/// handle_error(e)
|
||||
/// ```
|
||||
///
|
||||
/// If you actually need to catch an unknown error, use `Exception` which will
|
||||
/// catch regular program errors but not important system exceptions.
|
||||
///
|
||||
/// ```python
|
||||
/// def run_a_function(some_other_fn):
|
||||
/// try:
|
||||
/// some_other_fn()
|
||||
/// except Exception as e:
|
||||
/// print(f"How exceptional! {e}")
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://www.python.org/dev/peps/pep-0008/#programming-recommendations)
|
||||
/// - [Python: "Exception hierarchy"](https://docs.python.org/3/library/exceptions.html#exception-hierarchy)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue