Python context manager

Context managers handle setup and teardown automatically. AI uses them for files, database connections, and locks.

pythoncontext managerwith statementcleanup
python
class DatabaseConnection:
    def __init__(self, host: str):
        self.host = host
        self.connection = None

    def __enter__(self):
        print(f"Connecting to {self.host}")
        self.connection = {"host": self.host, "active": True}
        return self.connection

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("Closing connection")
        self.connection["active"] = False
        if exc_type is not None:
            print(f"Error occurred: {exc_val}")
            return False
        return True

with DatabaseConnection("localhost") as conn:
    print(conn["active"])
    raise ValueError("Something went wrong")

Question

What gets printed, and does the ValueError propagate after the with block?