Exception Attributes

UnicodeError.encoding — exception attribute

Inspect the codec name recorded by UnicodeEncodeError or UnicodeDecodeError. A bare UnicodeError has no encoding field; a UnicodeTranslateError has None for encoding on the tested runtime. Use the field for diagnostics, not to infer the correct encoding of arbitrary bytes.

Formal Syntax & Structure

python syntaxpython
error.encoding

Return Value

str | None (concrete codec errors)

Metadata on the concrete Unicode exception, when applicable.

Working Example

Example Snippetpython
try:    b"A\xffB".decode("utf-8")except UnicodeDecodeError as error:    print(error.encoding)
Python Reference