Python 异常处理

try-except 可以应用所有错误,同时也可以指定错误的类型进行过滤。

除非特殊需求,对于我来说就不需要特定的了。。。

#!/usr/bin/python

import sys

try:
    s=raw_input('Enter:')
except:
    sys.exit()

下面这个案例是简明教程的,但是一般来说,上面的够用了。特定的错误有的时候无法估计,除非你很懂。。

 #!/usr/bin/python
# Filename: try_except.py

import sys

try:
    s = raw_input('Enter something --> ')
except EOFError:
    print '\nWhy did you do an EOF on me?'
    sys.exit() 
except:
    print '\nSome error/exception occurred.'
print 'Done' 

 

posted @ 2013-03-07 12:18  墨迹哥's  阅读(191)  评论(0编辑  收藏  举报