How to properly close resources on errors?
Answer
•Use with/context managers.
•For multiple resources: with a() as x, b() as y:.
•In try/finally, close what doesn't support with.
•Log exceptions and context.
•Don't leave connections/files open.
Was this answer helpful?