下面介绍python经常使用的异常模块
python
AttributeError试图访问一个类中不存在的成员(包括:成员变量、属性和成员方法)而引起的异常shell
AttributeError:'Animal' object has no attribute 'age'
OSError是操做系统相关异常操作系统
FileNotFoundError:[Error 2] No such file or directory: 'abc.txt'
IndexError异常是访问序列元素时,下标索引超出取值范围所引起的异常code
IndexError: list index out of range
KeyError异常是试图访问字典里不存在的键时而引起的异常索引
>>> dict[104] Traceback(most recent call last): File"<pyshell#14>", line1, in <module> dict1[104] KeyError: 104
NameError是试图使用一个不存在的变量而引起的异常it
NameError: name 'value1' is not defined
TypeError是试图传入变量类型与要求的不符合时而发生的异常ast
>>> i = '2' >>> print(5 / i) Traceback(most recent call last): File"<pyshell#20>", line1, in <module> print(5 / i) TypeError: unsupported operand type(s) for /: 'int' and 'str'
ValueError异常是因为传入一个无效的参数值而引起的异常变量
>>> i = 'QWE >>> print(5 / int(i)) Traceback(most recent call last): File"<pyshell#22>", line1, in <module> print(5 / int(i)) ValueError: invalid literal for int() with base 10: 'QWE'