python 代码片段15
2016-01-04 20:27
711 查看
#coding=utf-8 ''' 如果运行时发生异常的话,解释器会查找相应的处理函数。要是在当前函数里没有 找到的话,它会将异常传递给上层的调用函数,看看那里能不能处理。如果在在最 外层还没有找到的话,解释器就会推出,同时打印出traceback以便让用户找出 错误产生的原因。 ''' try: f=open(filename,'r') except IOError,e: return False,str(e) #如果一个语句可以处理多个异常类型 try: process_some_data() except (TypeError,ValueError),e: print "ERROR:you provide invalid data",e # 或者 try: process_some_data() except (TypeError,ValueError),e: print "ERROR:you provide invalid data",e except ArithmeticError,e: print "ERROR:some math error occurred",e except Exception,e: print "ERROR:you provide invalid data",e # finally子句 try: get_mutex() do_some_stuff() finally: free_mutex()
相关文章推荐
- python 代码片段14
- python正则表达式去除两个特殊字符间的内容
- python 代码片段13
- python 代码片段12
- 一个简单的python 地址簿程序
- python setup.py install 和python setup.py develop的区别
- Python Base of Scientific Stack(Python基础之科学栈)
- Python Base of Scientific Stack(Python基础之科学栈)
- 进击python第三篇:基础
- Python:数据流中查找特定子串的简单算法
- Python OS模块
- python中的异常
- Python文件夹与文件的操作
- 批量备份脚本
- python之爬虫框架
- Python实现邮件发送
- Python列表的remove方法的注意事项
- Python机器学习库scikit-learn实践
- Python模块,glob 文件路径查找之删除过期日志
- Python列表,元组,字典,序列,引用