Python 运行时捕获 Warning

2023-01-31 05:01:07 python 运行 捕获

>>> import warnings
>>> 
>>> warnings.filterwarnings('error')
>>> 
>>> try:
...     warnings.warn(Warning())
... except Warning:
...     print 'Warning was raised as an exception!'
... 
Warning was raised as an exception!




参考资料:

1、Http://stackoverflow.com/questions/15933741/how-do-i-catch-a-warning-in-python-like-its-an-exception-not-just-for-testing

2、https://docs.Python.org/2/library/warnings.html


相关文章