do not raise base exception

このページは日本語には対応しておりません。随時翻訳に取り組んでいます。翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください。

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