all Python 3Exceptionsexception
UnicodeDecodeError
built-in exceptions
Bytes could not be decoded into a string, e.g. b'\xff'.decode('utf-8').
Syntax
raise UnicodeDecodeError
The bytes are rarely the real problem — the guessed encoding usually is: before Python 3.15, open() without encoding= falls back to the platform default, so a UTF-8 file that reads fine on Linux blows up on a Windows box using cp1251. Catching the exception seldom helps; pass encoding='utf-8' explicitly, or errors='replace'/'ignore' when broken bytes are genuinely expected. The instance carries encoding, object, start, end and reason, which pinpoint the exact byte where the decoder gave up.
Examples
try:
b'\xff'.decode('utf-8')
except UnicodeDecodeError as e:
print(type(e).__name__) # → UnicodeDecodeError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy