ruff/crates/ruff_linter/resources/test/fixtures/tryceratops/TRY301.py
2023-09-20 08:38:27 +02:00

66 lines
1.3 KiB
Python

"""
Violation:
Checks for `raise` statements within `try` blocks.
"""
class MyException(Exception):
pass
def bad():
try:
a = process()
if not a:
raise MyException(a)
raise MyException(a)
try:
b = process()
if not b:
raise MyException(b)
except Exception:
logger.exception("something failed")
except Exception:
logger.exception("something failed")
def bad():
try:
a = process()
if not a:
raise MyException(a)
raise MyException(a)
try:
b = process()
if not b:
raise MyException(b)
except* Exception:
logger.exception("something failed")
except* Exception:
logger.exception("something failed")
def good():
try:
a = process() # This throws the exception now
except MyException:
logger.exception("a failed")
except Exception:
logger.exception("something failed")
def fine():
try:
a = process() # This throws the exception now
finally:
print("finally")
def fine():
try:
raise ValueError("a doesn't exist")
except TypeError: # A different exception is caught
print("A different exception is caught")