def cacls(x, y):
try:
return x/y
except ZeroDivisionError:
print("y can not be zerp")
except TypeError:
print("TypeError")
print( cacls(10, 2) )
print( cacls(10, 0) )
print( cacls(10, "hello") )
#用一个块捕捉多个异常
def cacls_2(x, y):
try:
return x/y
except (ZeroDivisionError, TypeError):
print("exception occurt")
print( cacls_2(10, "hello") )
#捕获异常的对象
def cacls_3(x, y):
try:
return x/y
except (ZeroDivisionError, TypeError) as e:
print( e )
print( cacls_3(10, "hello") )
#捕获全部异常
def cacls_4(x, y):
try:
return x/y
except:
print("exception occurt ")
print( cacls_4(10, "hello") )