Raise warning in Python without interrupting program
By default, unlike an exception, a warning doesn't interrupt.
'This is a default warning.')<string>:1: UserWarning: This is a default warning.warnings.warn(
To simply use a preexisting class instead, e.g.
'This is a particular warning.', DeprecationWarning)<string>:1: DeprecationWarning: This is a particular warning.warnings.warn(
Creating a custom warning class is similar to creating a custom exception class:
class MyCustomWarning(UserWarning): passwarnings.warn('This is my custom warning.', MyCustomWarning)<string>:1: MyCustomWarning: This is my custom warning.
As an alternative, especially for standalone applications, consider the
logging module. It can log messages having a level of debug, info, warning, error, etc. Log messages having a level of warning or higher are by default printed to stderr.