热线电话:13121318867

登录
2018-12-13 阅读量: 725
用try-excep语句处理异常

在python中,你也可以在try-except块上使用else子句,它必须在所有except子句之后出现。仅当try子句不引发异常时,代码才会进入else块。

# Program to depict else clause with try-except

# Function which returns a/b

def AbyB(a , b):

try:

c = ((a+b) / (a-b))

except ZeroDivisionError:

print "a/b result in 0"

else:

print c

# Driver program to test above function

AbyB(2.0, 3.0)

AbyB(3.0, 3.0)

上述程序的输出将是:

0.0000
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子