If a context manager's enter method raises an exception, what happens to its exit method?

  • It depends on Python's version.
  • It's called after enter regardless of exceptions.
  • It's called only if enter completes without exceptions.
  • It's not called at all.
If the enter method raises an exception, the exit method is not called at all, ensuring resource cleanup doesn't happen in case of an error during setup.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *