python中的告警处理

在Python中,遇到异常时,一类是直接抛出异常,exception;另一类直接告警warning.

对于后者,通常是打印一句话。前者则或中断程序执行。

考虑到避免由于告警导致后续的不可预知的错误,可以对warning进行异常处理。

1. 不让打印告警信息

import warnings
warnings.filterwarnings("ignore")


2. 当异常处理,用except捕获。

import warnings
warnings.filterwarnings("error")
try:
  ...
except:
  ...

详细处理方式

Value Disposition
"error" turn matching warnings into exceptions
"ignore" never print matching warnings
"always" always print matching warnings
"default" print the first occurrence of matching warnings for each location where the warning is issued
"module" print the first occurrence of matching warnings for each module where the warning is issued
"once" print only the first occurrence of matching warnings, regardless of location

如何处理

参考

  1. Python 官方文档-warnings
  2. Python 官方文档--warning filter

posted on 2016-10-25 09:55  rainduck  阅读(2450)  评论(0编辑  收藏  举报

导航