[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

posted @ 2021-07-29 09:05  cxc1357  阅读(27)  评论(0编辑  收藏  举报