Python中异常处理简单示例(try,except,finally)

时间:2022-12-15 13:40:01
#-*-coding:utf-8-*-
import sys
def Main():
	try:
		f=open('firstpython.py')
		s=f.readline()
		print s
	except IOError,(errno,strerror):
		print "I/O error(%s):%s" %(errno,strerror)
	except ValueError:
		print "could not convert data to an integer"
	except:
		print "Unexpected error:",sys.exc_info()[0]
		raise
	finally:
		f.close()
if __name__=="__main__":
	Main()