Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

__exit__

__exit__ is the context-manager hook Python invokes when leaving a with block. It receives exception details when the block fails and can suppress the exception by returning a true value.

How it works

When a with block finishes, Python calls the context manager’s __exit__ method. It receives the exception type, value, and traceback when an exception caused the block to end, or three None values on normal completion; a true return suppresses that exception.

Syntax

def __exit__(self, exc_type, exc_value, traceback):

Parameters

NameTypeDescription
exc_typetype[BaseException] | NoneException class, or None when no exception caused the exit.
exc_valueBaseException | NoneException instance, or None on normal completion.
tracebacktraceback | NoneTraceback object, or None on normal completion.

Returns

A truthy value suppresses an exception from the with suite; a false value allows it to be reraised. On non-exception exits, the return value is ignored.

Key points

  • Python invokes __exit__ whenever execution leaves the with suite.
  • An exception exit supplies the exception class, instance, and traceback to the method.
  • A normal exit calls __exit__ with None for each exception-related argument.
  • After suppression, control resumes after the complete with statement.

Common mistakes

  • Returning true has no effect when the with block ends normally or through a non-exception control-flow exit.
  • A truthy return hides the active exception, so suppress only errors the context manager can deliberately handle.

Example: Suppressing a selected exception

class Managed:
    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        return exc_type is ValueError

with Managed():
    raise ValueError("handled")

The context manager suppresses a ValueError raised inside the with block.

When to use it

Deterministic cleanup

Release a lock, file, socket, or other resource when a with block ends.

Selective exception suppression

Convert a specific, expected exception into normal control flow.

Failure logging

Record exception details while still allowing unexpected failures to propagate.

Interview questions

QWhen is __exit__ called, and what arguments does it receive?

Python calls __exit__ when the with suite ends. On an exception, it receives the exception type, value, and traceback; otherwise all three arguments are None.

Follow-up: What does a false return do after an exception? The exception is reraised, preserving normal error propagation.

QHow can __exit__ suppress an exception?

A truthy return from __exit__ suppresses an exception raised in the with suite. A false return causes that exception to be reraised.

Follow-up: Is the return value used on normal completion? No; it is ignored when no exception caused the suite to exit.

QWhat role does __exit__ play in context-manager cleanup?

A context manager’s __exit__ method is invoked as part of leaving a with statement. Its return value controls only exception suppression, not ordinary completion.

Follow-up: What should __exit__ return when cleanup should not handle errors? Return a false value, commonly None, so exceptions propagate.

  • __enter__
  • with statement
  • context manager
  • contextlib.AbstractContextManager
  • contextlib.ExitStack

Source: 8.5. The with statement. Checked 2026-10-16.