编程语言
首页 > 编程语言> > [Python] 异常

[Python] 异常

作者:互联网

例1:定义一个函数计算除法,若除数为零,则显示异常

def myFunc(x,y):
    if y == 0:
        raise ValueError('y cannot be zero')
    else:
        return x/y
temp = 100
velo = 0
try:
    print(myFunc(temp,velo))
except ValueError as error:
    print(error)
>>> y cannot be zero

 

参考:

https://blog.csdn.net/qq_41841569/article/details/83063431

标签:velo,myFunc,Python,zero,cannot,print,异常,ValueError
来源: https://www.cnblogs.com/cxc1357/p/10395278.html