Built-in Exceptions
UserWarning — exception class
Default category for warnings emitted by application code. Use it for a nonfatal situation the user should know about; reserve exceptions for operations that cannot complete correctly. The example explicitly promotes this warning category to an exception to make the result independent of default filters. Catch UserWarning when this category is the recovery boundary; do not rely on the exact human-readable message.
Formal Syntax & Structure
python syntaxpython
UserWarningReturn Value
type
The class object UserWarning; this reference documents the class identity, not a universal constructor signature.
Working Example
Example Snippetpython
import warningstry: with warnings.catch_warnings(): warnings.simplefilter("error", UserWarning) warnings.warn("demo", UserWarning)except UserWarning as error: print(type(error).__name__)