Context managers and resource cleanup

Does leaving the block still close the file?

Examples

Worked example

Input
An exception is raised inside with open(...).
Expected output
Yes.

Hints and solution

Hint 1

Write down the starting state and walk through one example by hand.

Reveal worked answer

The file object’s context manager runs its exit behavior while the exception leaves the block.

Back to the lesson