Python daily--sample of Exception
来源:互联网 发布:重庆大数据招聘 编辑:程序博客网 时间:2024/06/06 00:54
一个异常处理的例子:
#!/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() # exit the program
except:
print '\nSome error/exception occurred.'
# here, we are not exiting the program
print 'Done'
例子2
#!/usr/bin/python
# Filename: finally.py
import time
try:
f = file('\poem.txt')
while True: # our usual file-reading idiom
line = f.readline()
if len(line) == 0:
break
time.sleep(2)
print line,
except KeyboardInterrupt:
print ('\nWhy did u do an EOF on me?')
finally:
f.close()
print('Cleaning up...closed the file')
执行结果:<当打印的时候点击ctrl+c>
>>> ================================ RESTART ================================
>>>
sean
Why did u do an EOF on me?
Cleaning up...closed the file
>>>
- Python daily--sample of Exception
- Daily Build Script Sample
- exception handling of Python
- Notes of Daily Work
- The daily use of sql2008
- A sample of Generics.
- A sample of indexers.
- a sample of Interators
- Sample of BDC
- sample code of webview
- sample of google earth
- Usage sample of SSL
- sample of spool & sqlldr
- Sample of API FND_PROFILE
- sample of day
- Python sample code of hudson remote api, my first Python hello world
- Daily Python 1: 元组转字典
- python code segment daily collect
- SDP培训
- sql数据库常见的应用语句
- mysql示例employees数据库
- C++ 标准输出如何控制小数点后位数
- svn 亲自实践操作
- Python daily--sample of Exception
- 黑马程序员—File类的知识的总结及练习
- vim下自动排版命令
- Segment fault
- 构建嵌入式Linux根文件系统jffs2
- 简单解释尾递归
- 开发人员必学的5门课程
- PHP生成唯一标识ID
- 微信公众平台开发(八) 自定义菜单功能开发