python中的告警處理

在Python中,遇到異常時,一類是直接拋出異常,exception;另外一類直接告警warning.html

對於後者,一般是打印一句話。前者則或中斷程序執行。python

考慮到避免因爲告警致使後續的不可預知的錯誤,能夠對warning進行異常處理。less

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

如何處理code

參考

  1. Python 官方文檔-warnings
  2. Python 官方文檔--warning filter
相關文章
相關標籤/搜索