Python Glossary Русский

all Python 3Exceptionsexception

UnicodeError

built-in exceptions

Base class for Unicode encoding/decoding errors (a subclass of ValueError).

Syntax

raise UnicodeError

It is the common ancestor of UnicodeDecodeError and UnicodeEncodeError, so a single except UnicodeError covers both directions — at the cost of not telling you which one failed; catch the specific class when the handling differs. It also inherits from ValueError, so an earlier except ValueError will swallow it, which is easy to miss when reading a chain of handlers. Occasionally UnicodeError is raised on its own — the undefined codec does so for any string, and idna for an unsupported errors mode; an empty or over-long domain label in idna, however, raises the UnicodeEncodeError subclass.

Examples

try:
    raise UnicodeError
except UnicodeError as e:
    print(type(e).__name__)   # → UnicodeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy