mirror of
https://github.com/astral-sh/ruff
synced 2026-01-11 08:34:29 -05:00
doc: add documentation for TRY002 (#2655)
This commit is contained in:
44
docs/rules/raise-vanilla-class.md
Normal file
44
docs/rules/raise-vanilla-class.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# raise-vanilla-class (TRY002)
|
||||
|
||||
Derived from the **tryceratops** linter.
|
||||
|
||||
### What it does
|
||||
Checks for code that raises `Exception` directly.
|
||||
|
||||
### Why is this bad?
|
||||
Handling such exceptions requires the use of `except Exception`, which
|
||||
captures _any_ raised exception, including failed assertions,
|
||||
division by zero, and more.
|
||||
|
||||
Prefer to raise your own exception, or a more specific built-in
|
||||
exception, so that you can avoid over-capturing exceptions that you
|
||||
don't intend to handle.
|
||||
|
||||
### Example
|
||||
```py
|
||||
def main_function():
|
||||
if not cond:
|
||||
raise Exception()
|
||||
def consumer_func():
|
||||
try:
|
||||
do_step()
|
||||
prepare()
|
||||
main_function()
|
||||
except Exception:
|
||||
logger.error("Oops")
|
||||
```
|
||||
|
||||
Use instead:
|
||||
```py
|
||||
def main_function():
|
||||
if not cond:
|
||||
raise CustomException()
|
||||
def consumer_func():
|
||||
try:
|
||||
do_step()
|
||||
prepare()
|
||||
main_function()
|
||||
except CustomException:
|
||||
logger.error("Main function failed")
|
||||
except Exception:
|
||||
logger.error("Oops")
|
||||
38
docs/rules/raise_vanilla_class.md
Normal file
38
docs/rules/raise_vanilla_class.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# raise-vanilla-class (TRY002)
|
||||
Derived from the **tryceratops** linter.
|
||||
|
||||
### What it does
|
||||
Checks for bare exceptions.
|
||||
|
||||
## Why is this bad?
|
||||
It's hard to capture generic exceptions making it hard for handling specific scenarios.
|
||||
|
||||
## Example
|
||||
```py
|
||||
def main_function():
|
||||
if not cond:
|
||||
raise Exception()
|
||||
def consumer_func():
|
||||
try:
|
||||
do_step()
|
||||
prepare()
|
||||
main_function()
|
||||
except Exception:
|
||||
logger.error("I have no idea what went wrong!!")
|
||||
```
|
||||
|
||||
## How it should be
|
||||
```py
|
||||
def main_function():
|
||||
if not cond:
|
||||
raise CustomException()
|
||||
def consumer_func():
|
||||
try:
|
||||
do_step()
|
||||
prepare()
|
||||
main_function()
|
||||
except CustomException:
|
||||
logger.error("Main function failed")
|
||||
except Exception:
|
||||
logger.error("I have no idea what went wrong!!")
|
||||
```
|
||||
Reference in New Issue
Block a user