Built-in Exceptions

UnicodeDecodeError — exception class

A byte sequence is invalid for the selected text decoder. The failed span indexes the input bytes, not Unicode characters. Check the source encoding and whether a streamed byte sequence is complete before choosing a replacement policy. Catch UnicodeDecodeError when this category is the recovery boundary; do not rely on the exact human-readable message.

Formal Syntax & Structure

python syntaxpython
UnicodeDecodeError

Return Value

type

The class object UnicodeDecodeError; this reference documents the class identity, not a universal constructor signature.

Working Example

Example Snippetpython
try:    bytes([255]).decode("utf-8")except UnicodeDecodeError as error:    print(type(error).__name__)
Python Reference