热线电话:13121318867

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

try语句可以有多个except子句,以指定不同异常的处理程序。请注意,最多只会执行一个处理程序。

# Program to handle multiple errors with one except statement

try :

a = 3

if a < 4 :

# throws ZeroDivisionError for a = 3

b = a/(a-3)

# throws NameError if a >= 4

print "Value of b = ", b

# note that braces () are necessary here for multiple exceptions

except(ZeroDivisionError, NameError):

print "\nError Occurred and Handled"

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

发表评论

暂无数据
推荐帖子