Title: Catch except in __enter__()
Tags: python,class,oop,contextmanager

Question: I wanted to try using the context manager mode. They say he's better. It can also catch exceptions and is better readable.
with my_class() as some:
    some.do()

The examples I found all look like this:
class Exm:
    def __init__(self):
        some_data_get
        connection = connection_to_resource_good   # for example 1

    def __enter__(self):
        connection = connection_to_resource_good    # for example 2
        return connection

    def __exit__(self, exc_type, exc_val, exc_tb):
        connection.close()
        return some_True    # for continue code if some error in some.do()


But in the examples I've seen, no one catches an exception when accessing a resource. If there is an error in the connection, then this will break the continuation of the code
class My:
    def __init__(self):
        print("INIT")

    def __enter__(self):
        connection = 1 / 0      # error bad connection
        return connection

    def __exit__(self, exc_type, exc_val, exc_tb): # not call - error in init or enter
        connection.close()
        return some_True    # for continue code if some error in some.do()

Here is a more specific example:
class DB:

    def __init__(self, host, user, password, db_name):
        self.host = host
        self.user = user
        self.password = password
        self.db_name = db_name

    def __enter__(self):
        self.connection = name_some_module_for_sql.connection(self.host, self.user, self.password, self.db_name)
        self.cursor = self.connection.cursor()
        return self.cursor

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.cursor.close()
        self.connection.close()
        return True  # for continue code

for an example in the code, you need to insert in three places
with DB(1, 1, 1, 1) as cursor:  # into db_1
    cursor.insert_to_db_word("Hello")
    cursor.insert_to_db_word("My")
    cursor.insert_to_db_word("Dear")
    cursor.insert_to_db_word("Friend")

with DB(2, 2, 2, 2) as cursor:  # into db_2
    cursor.insert_to_db_word("Hello")
    cursor.insert_to_db_word("My")
    cursor.insert_to_db_word("Dear")
    cursor.insert_to_db_word("Friend")

with DB(3, 3, 3, 3) as cursor:  # into db_3
    cursor.insert_to_db_word("Hello")
    cursor.insert_to_db_word("My")
    cursor.insert_to_db_word("Dear")
    cursor.insert_to_db_word("Friend")

How can I handle exceptions in the init or enter functions so that the cursor.insert_to_db_word functions are not called and the program code continues to be executed if there is a problem connecting to the first database?
Only for class
P.S. sorry for google translete
Before that, I implemented it with decorators. I wanted to use a class for beautiful code and readability

Answer: __exit__ is not there for "suppressing" the exceptions. Its main idea is to ensure that there is a piece of code which always gonna be executed even if an exception occurs "inside" that block of code. Since an exception might occur it gives the ability to inspect that object and possibly suppress it by returning True in some situations.
With that being said I think it doesn't make sense for __enter__ to also have this ability to suppress exceptions. You need try-except block for that inside __enter__(like anywhere else in the code).