Can an insert in a dictionary raise KeyError?

Can this code generate a KeyError? I am new to python, I just want to clarify, since a verified insert cannot throw a KeyError exception.

def add(self, name, company):
    try:
        self.identity[self.id] = { 'name': name,
                                   'company': company }
        return self.identity
    except KeyError:
        raise NameNotAdded

Here, identity is a dictionary and id is an initialized int elsewhere and NameNotAdded is a custom exception.

+4
source share
2 answers

KeyError, TypeError, , , . Python , a TypeError.

, dict, MemoryError - OverflowError, ( sys.maxsize). , .:)

+2

KeyError.

A KeyError , . .

+3

Source: https://habr.com/ru/post/1610182/


All Articles