Built-in Exceptions

Warning — exception class

Root of the warning-category hierarchy, itself an Exception subclass. Warnings are normally emitted through warnings.warn, and filters control whether they are shown, ignored or raised. The example explicitly promotes this warning category to an exception to make the result independent of default filters. Catch Warning when this category is the recovery boundary; do not rely on the exact human-readable message.

Formal Syntax & Structure

python syntaxpython
Warning

Return Value

type

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

Working Example

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