UnicodeDecodeError

Built-in ExceptionPython 2.0+Intermediate

Raised when decoding bytes to a string fails

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
# Triggering and catching UnicodeDecodeError
try:
    b"\xff".decode("ascii")
except UnicodeDecodeError as e:
    print(f"Caught UnicodeDecodeError: {e}")
    print(f"Type: {type(e).__name__}")

UnicodeDecodeError is raised when raised when decoding bytes to a string fails. Always catch specific exceptions rather than bare except clauses.

Try in Playground

Tags

exceptionerror-handlingcore