UnicodeDecodeError, invalid continuation byte

Here is an example of a Python code snippet that could cause a UnicodeDecodeError: invalid continuation byte error:

def decode_text(text):
    try:
        return text.decode('utf-8')
    except UnicodeDecodeError:
        return "Invalid continuation byte"

invalid_text = b'\x80abc'
decoded_text = decode_text(invalid_text)
print(decoded_text)

Watch a course Python - The Practical Guide

In this example, the invalid_text variable contains a byte sequence that is not valid UTF-8 encoded text. The decode_text function attempts to decode this text using the UTF-8 encoding, but the decoding process raises a UnicodeDecodeError because the byte 0x80 is not a valid continuation byte in the UTF-8 encoding. The error message is then caught and handled by the except block, which returns the string "Invalid continuation byte" instead.