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:
Trevor Gross 2023-06-10 18:42:43 -04:00 committed by GitHub
parent 445e1723ab
commit 9f7cc86a22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)