Python入门教程 错误和异常

def div(a,b):
    return a/b

try:
    div(1,0)
except XeroDivsionError
    print("Oops")

def div(a,b):
    try:
        result = a/b
    except ZeroDivisionError as e:
        print(e)
    else:
        print("aaaa",result)
    finally:
        print("bbb")

##
>>> div(2,0)
division by zero
bbb
>>> div(1,2)
aaaa 0.5
bbb

https://docs.python.org/3/tutorial/errors.html

猜你喜欢

转载自blog.csdn.net/weixin_42199275/article/details/81485287