Built-in Exceptions

EncodingWarning — exception class

Warns about encoding-related issues, notably implicit default text encodings when opt-in encoding warnings are enabled. Explicit encoding choices make file handling more predictable. The example explicitly promotes this warning category to an exception to make the result independent of default filters. Catch EncodingWarning when this category is the recovery boundary; do not rely on the exact human-readable message.

Formal Syntax & Structure

python syntaxpython
EncodingWarning

Return Value

type

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

Working Example

Example Snippetpython
import warningstry:    with warnings.catch_warnings():        warnings.simplefilter("error", EncodingWarning)        warnings.warn("demo", EncodingWarning)except EncodingWarning as error:    print(type(error).__name__)
Python Reference