do not raise base exception

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

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