do not raise base exception

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: python-best-practices/no-base-exception

Language: Python

Severity: Notice

Category: Best Practices

Description

Do not raise Exception and BaseException. These are too generic. Having generic exceptions makes it difficult to differentiate errors in a program. Use a specific exception, for example, ValueError, or create your own instead of using generic ones.

Learn More

Non-Compliant Code Examples

if foo:
	raise Exception("bla")
elif bar:
	raise Exception
else:
	raise Exception
def use_base_exception():
	raise Exception
	raise Exception("awesome")
for v in list:
	raise BaseException

Compliant Code Examples

if foo:
	print("bar")
else:
	raise ValueError
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis