Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

TypeError — an operation on an object of an inappropriate type

TypeError means that an operation or function received an object of an inappropriate type. Learn how it differs from ValueError and how to handle it without hiding the underlying problem.

Version: Python 3.14; repository image, production runtime not attested

Syntax

raise TypeError("message")

try:
    operation()
except TypeError as exc:
    print(exc)

TypeError is a built-in exception raised when an object's type is inappropriate for an operation or function. For example, "7" + 3 does not automatically combine text with a number.

Distinguish an inappropriate type (TypeError) from an acceptable type with an inappropriate value (ValueError). Your code can use raise TypeError("message") when an operation is unsupported for an object. By contrast, NotImplementedError concerns an operation intended to be supported whose implementation is still missing.

When diagnosing the error, inspect the message and traceback to locate the failing operation. Catch TypeError only where you can respond meaningfully; do not hide unexpected errors with a broad except Exception block.

In the example, except TypeError handles the failed operation. Then int("7") + 3 produces a numeric sum, while "7" + str(3) concatenates strings. Choose the conversion that matches the program's intent rather than merely removing the exception.

Examples

try:
    result = "7" + 3
except TypeError:
    print("TypeError")

print(int("7") + 3)
print("7" + str(3))

Result

TypeError
10
73

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources