如何解决:W602 已弃用的引发异常的形式 [英] How to fix: W602 deprecated form of raising exception
问题描述
如果我使用 pylint(通过 sublimerlinter),我会收到以下警告消息:
If I use pylint (via sublimerlinter) I get following warning message:
W602 不推荐使用的引发异常的形式
这就是我在代码中使用异常的方式:
This I how I use exceptions in my code:
if CONDITION == True:
raise ValueError, HELPING_EXPLANATION
推荐答案
像这样提出你的异常:
if CONDITION == True:
raise ValueError(HELPING_EXPLANATION)
来自 PEP 8 -- Python 代码风格指南 - 编程建议一个>:
引发异常时,使用 raise ValueError('message')
而不是旧形式 raise ValueError, 'message'
.
When raising an exception, use raise
ValueError('message')
instead of the older formraise ValueError, 'message'
.
首选使用括号的形式,因为当异常参数很长或包含字符串格式时,由于包含括号,您不需要使用行继续符.旧形式将在 Python 3 中删除.
The paren-using form is preferred because when the exception arguments are long or include string formatting, you don't need to use line continuation characters thanks to the containing parentheses. The older form will be removed in Python 3.
这篇关于如何解决:W602 已弃用的引发异常的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!