python中的异常
来源:互联网 发布:sql server字符串类型 编辑:程序博客网 时间:2024/06/09 16:18
1,空的except语句将会捕捉所有的异常,可以用sys模块中取出异常名和异常的值
2,raw_input()读文件到末尾时,会引发EOFError异常,这种异常不是错误
3,finally只做清楚工作,不做异常处理
异常处理的例子:
myException="Error"def raise1(): raise myException, "hello"def raise2(): raise myExceptiondef tryer(func): try: func() except myException,extraInfo: import sys print sys.exc_type print "got this:",extraInfo
执行的例子:
from exc import *tryer(raise1)tryer(raise2)
报错:TypeError: exceptions must be old-style classes or derived from BaseException, not str
原因:In Python 2.5 and below, your code would work, as then it was allowed to raise strings as exceptions. This was a very bad decision, and so removed in 2.6.
改写后的例子:
class myException(Exception): passdef raise1(): raise myException, "hello"def raise2(): raise myExceptiondef tryer(func): try: func() except myException,extraInfo: import sys print sys.exc_type print "got this:",extraInfo
让自己编写的异常类全部继承于Exception顶级异常类就行了
阅读全文
0 0
- Python中的异常类型
- Python中的一些异常
- python中的异常
- python 中的异常
- Python中的异常类型
- Python中的异常类型
- Python中的异常处理
- python中的异常总结
- python中的异常处理
- Python中的异常
- python中的异常处理
- python中的异常处理
- python中的异常
- python 中的异常处理
- Python中的异常类型
- python中的异常
- python中的异常
- python中的异常
- 关于“日志”的一点想法
- PHP培训机构排名
- RTMP网页视频抓取
- iOS 性能优化总结 (多贴总结)
- 图片上传显示进度条和预览图的前端实现之进度条篇
- python中的异常
- A+B for Input-Output Practice (III)
- Linux中profile、bashrc、bash_profile之间的区别和联系
- Unity5.2在VS2015中调试
- android中接口实现更新UI方法
- JAVA字符串格式化-String.format()的使用
- PHP程序员的进阶之路
- Mybatis 和 自定义构造函数 冲突的解决办法
- ps -ef 和ps -aux 和ps aux