Python引发异常

示例

如果您的代码遇到一个不知道如何处理的条件(例如参数不正确),它将引发相应的异常。

def even_the_odds(odds):
    if odds % 2 != 1:
        raise ValueError("Did not get an odd number")
    
    return odds + 1