Built-in Exceptions

SyntaxWarning — exception class

Reports suspicious source constructs, often at compilation rather than execution. It differs from SyntaxError: the source may still compile when warnings are not promoted to errors. The example explicitly promotes this warning category to an exception to make the result independent of default filters. Catch SyntaxWarning when this category is the recovery boundary; do not rely on the exact human-readable message.

Formal Syntax & Structure

python syntaxpython
SyntaxWarning

Return Value

type

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

Working Example

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