From 9f7cc86a22fbfc3276ff60d4bad25d5b3b7ac3de Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Sat, 10 Jun 2023 18:42:43 -0400 Subject: [PATCH] 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. --- .../ruff/src/rules/pycodestyle/rules/bare_except.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs b/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs index f2d591e061..42adebdc54 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs @@ -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)