错误和异常

给代码添加错误检测以及异常处理,只要把代码封装在try-except语句中,try之后的代码,就是打算管理的代码except之后的代码,则是你处理错误的代码。

try:

  filename=raw_input("please input fliename")

  fobj=open(filename,'r')

  for eachLine in fobj:

    print eachLine

except IOError,e:

  print 'file open error',e

程序员可以故意通过raise语句制造一个错误  。

posted @ 2015-05-11 16:53  ElliotLiu  阅读(113)  评论(0)    收藏  举报