이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

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
PREVIEWING: dgreen15/github-error-fix