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

Metadata

ID: python-best-practices/no-bare-except

Language: Python

Severity: Warning

Category: Best Practices

Description

Avoid bare except. Try to always use specialized exception names in except blocks.

Non-Compliant Code Examples

try:
  print("foo")
except:  # use a specialized exception name
  print("bar")

Compliant Code Examples

try:
    parsed = json.loads(response.body)
except json.JSONDecodeError:
    log.warning("Test skips request responded with invalid JSON '%s'", response.body)
    return
try:
  pass
except (TypeError, ValueError):
    log.debug(
        (
            "received invalid x-datadog-* headers, "
            "trace-id: %r, parent-id: %r, priority: %r, origin: %r, tags:%r"
        ),
        trace_id,
        parent_span_id,
        sampling_priority,
        origin,
        tags_value,
    )
try:
    foo()
except MyError as e:
    bar()
try:
  print("foo")
except MyException:
  print("bar")
PREVIEWING: aliciascott/DOCS-9725-Cloudcraft