例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