Python 常用的异常类型
2014-06-24 00:30
302 查看
Python中的异常类型
转自 http://blog.csdn.net/fcoolx/archive/2009/05/20/4202872.aspx 1、NameError:尝试访问一个未申明的变量>>> v
NameError: name 'v' is not defined
2、ZeroDivisionError:除数为0
>>> v = 1/0
ZeroDivisionError: int division or modulo by zero
3、SyntaxError:语法错误
>>> int int
SyntaxError: invalid syntax (<pyshell#14>, line 1)
4、IndexError:索引超出范围
>>> List = [2]
>>> List[3]
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
List[3]
IndexError: list index out of range
5、KeyError:字典关键字不存在
>>> Dic = {'1':'yes', '2':'no'}
>>> Dic['3']
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
Dic['3']
KeyError: '3'
6、IOError:输入输出错误
>>> f = open('abc')
IOError: [Errno 2] No such file or directory: 'abc'
7、AttributeError:访问未知对象属性
>>> class Worker:
def Work():
print("I am working")>>> w = Worker()
>>> w.a
Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
w.a
AttributeError: 'Worker' object has no attribute 'a'
8、ValueError:数值错误
>>> int('d')
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
int('d')
ValueError: invalid literal for int() with base 10: 'd'
9、TypeError:类型错误
>>> iStr = '22'
>>> iVal = 22
>>> obj = iStr + iVal;
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
obj = iStr + iVal;
TypeError: Can't convert 'int' object to str implicitly
10、AssertionError:断言错误
>>> assert 1 != 1
Traceback (most recent call last):
File "<pyshell#70>", line 1, in <module>
assert 1 != 1
AssertionError
本文出自 “君志所向,一往无前。” 博客,谢绝转载!
相关文章推荐
- c# 中异常捕获catch{}常用的异常类型
- 判断异常类型,全面捕获Python的异常
- python异常类型
- python中常用的列表类型内建函数
- python中用try来处理程序异常的集中常用方法
- c#中异常捕获catch{}常用的异常类型
- python3 数字类型常用函数
- Python 异常类型
- python学习六:异常处理、map/reduce/filter内置函数、python集合(set)类型、元组转列表
- python 判断字符串中字符类型的常用方法
- catch{}常用的异常类型
- 2013-08-11 Hello word|异常|代码书写规范|编译器虚拟机|注释|命名规则|数据类型|不常用的运算符|语法|
- python中用try来处理程序异常的集中常用方法
- python 异常类型
- .NET常用的异常类型及其中文说明
- Python中的异常类型
- struts2 常用的结果类型,动态结果,异常处理,如何传递参数与常用的通配符映射
- python 判断字符串中字符类型的常用方法
- 常用python数据类型转换函数总结
- 常见的Python异常类型