Throw From Finally (ARCH2155)
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
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.
Incorrect
Section titled “Incorrect”def write(path, data): f = open(path, "w") try: f.write(data) finally: f.close() raise RuntimeError("cleanup done")Correct
Section titled “Correct”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.