تخطَّ إلى المحتوى

Throw From Finally (ARCH2155)

هذا المحتوى غير متوفر بلغتك بعد.

Key python.logging.raise-in-finally · Severity error · Category logging

Raising an exception inside a finally block masks any exception still propagating from the try body. The original failure is lost and replaced by the secondary error from finally.

def write(path, data):
f = open(path, "w")
try:
f.write(data)
finally:
f.close()
raise RuntimeError("cleanup done")
def write(path, data):
f = open(path, "w")
try:
f.write(data)
finally:
f.close()

Use finally only for cleanup; move any raise into the try/except body.