#相对于终止程序的异常,有一些不那么异常的情况,只是需要注意或警告
import warnings#利用warnings模块
warnings.warn("I must to warning you")
# e:/python项目/基础训练/8.异常-8.Warn.py:3: UserWarning: I must to warning you
# warn("I must to warning you")
#UserWarning是引发的异常,可指定将引发的异常,只要是Warning的子类
#如果想要设计一种异常过滤的方式,可以使用filterwarning
warnings.filterwarnings("error")
warnings.warn("This function is too old",DeprecationWarning)
# e:/python项目/基础训练/8.异常-8.Warn.py:3: UserWarning: I must to warning you
# warn("I must to warning you")
# Traceback (most recent call last):
# File "e:/python项目/基础训练/8.异常-8.Warn.py", line 10, in <module>
# filterwarning("error")
# NameError: name 'filterwarning' is not defined
#filterwarnings(“error”)让本来只是警告变为了错误,终止程序,所以你也可以定义一些warn变为警告
warnings.filterwarnings("ignore",category=DeprecationWarning)
warnings.warn("This function is too old",DeprecationWarning)
#什么也不会发生,因为filterwarnings忽略了这种错误