热线电话:13121318867

登录
2018-12-13 阅读量: 656
pyhton怎么处理异常?

与其他语言一样,python还在try-except的帮助下通过异常处理方法提供运行时错误。一些最常见的标准异常包括IndexError,ImportError,IOError,ZeroDivisionError,TypeError。

异常是python中所有异常的基类。

让我们尝试访问索引超出范围的数组元素并处理相应的异常。

# Python program to handle simple runtime error

a = [1, 2, 3]

try:

print "Second element = %d" %(a[1])

# Throws error since there are only 3 elements in array

print "Fourth element = %d" %(a[3])

except IndexError:

print "An error occurred"

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

发表评论

暂无数据
推荐帖子