python Exception
2016-01-29 11:26
405 查看
python 常见的异常:
NameErrorZeroDivisionError
SyntaxError
Index Error
KeyError
AttributeError
ValueError
IOError
TypeError
捕获异常:
try: A except (Exception1, Exception2),e: print str(e) except (Exception3, Exception4),e: print str(e) # 捕获所有的异常 except Exception,e: print str(e) else: B # 无论有无异常都会执行 final: C
上下文管理 with
好处: 可以再任务结束时释放资源如:文件(数据,日志,数据库),线程资源,简单同步,数据库连接
使用with,首先要引入from__future__import with_statement
如:
with open('text.txt','r') as file: for eachLine in file: # do something
如有有什么异常,也会最后close文件
相关文章推荐
- python分布式任务调度Celery
- 使用cx_Freeze打包Python程序
- 谈谈python的GIL、多线程、多进程
- Python 3.x基于Svn补丁日志自动生成Java项目补丁包
- python新手必碰到的问题---encode与decode,中文乱码
- 【LEETCODE】242-Valid Anagram [Python]
- python常见的错误类型和继承关系
- 建立Scrapy项目unicodeDecodeError_ascii错误的解决 (2014-10-22)
- python爬虫获取google镜像
- python 变量命名规范
- python3基础语法
- 零基础学习PDB命令行调试Python代码
- 常用的python库(不断更新)
- python2.7_2.2_在套接字服务器上使用ForkingMixIn
- python基础:三层循环
- python与pexpect实现ssh操作
- Python twisted article
- selenium_python系列之二 Python启动浏览器Firefox\Chrome\IE
- 21行python代码实现拼写检查器
- python读网页解析内容写入文件