If using generic exception, it should be last

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: python-best-practices/generic-exception-last

Language: Python

Severity: Notice

Category: Best Practices

Description

When multiple exceptions are caught, the generic Exception must be caught last. Catching Exception is very generic and if placed before specific exceptions, it will caught all exceptions and specific exception handlers will not be caught.

For this reason, generic Exception must be the last to be handled to let specific exception handlers to be triggered/executed.

Learn More

Non-Compliant Code Examples

try:
	pass
except Exception:
	pass
except FileNotFound as e:
	pass

Compliant Code Examples

try:
	pass
except MyError:
	pass
except Exception as e:
	pass
try:
	pass
except MyError:
	pass
except FileNotFound as e:
	pass
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

PREVIEWING: Cyril-Bouchiat/add-vm-package-explorer-doc