[Language]Python中错误与异常

来源:互联网 发布:win10 mac地址怎么查 编辑:程序博客网 时间:2024/06/06 17:18

简述

Python允许程序运行时检测错误,可以抛出异常并进行处理。
代码中添加错误检测及异常处理,只要将代码放在try-except语句中,try之后的代码是要管理的代码,except之后的代码是错误发生时处理错误的代码。

try:    filename = raw_input('Enter file name: ')    fobj = open(filename, 'r')    for eachLine in fobj:        print(eachLine)    fobj.close()except IOError:    print("file open error")
0 0