章节索引 :

Python 中的错误和异常

Python 程序的执行过程中,当发生错误时会引起一个事件,该事件被称为异常。例如:

  • 如果程序中有语法错误,会产生 SyntaxError 类型的异常
  • 执行除以 0 的运算,会产生 ZeroDivisionError 类型的异常
  • 打开一个不存在的文件,会产生 IOError 类型的异常

编程中常见的异常类型总结如下:

异常名称 描述
ZeroDivisionError 除(或取模)零
AssertionError 断言语句失败
AttributeError 对象没有这个属性
FileNotFoundError 文件不存在
ModuleNotFoundError 模块不存在
IndexError 序列中没有此索引(index)
KeyError 映射中没有这个键
NameError 未声明/初始化对象
SyntaxError Python
IndentationError 缩进错误

1. ZeroDivisionError 的出现场景

进行除法运算时,要求被除数不能是 0,如果被除数是 0,则会产生异常,示例代码如下:

>>> 100 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
  • 在第 4 行,因为被除数是 0,产生 ZeroDivisionError 类型的异常

2. AssertionError 的出现场景

编写代码时,常常需要在某些特定的位置做出一些假设,假设某些条件为真,Python 使用 assert 语句假设指定条件为真:

assert 布尔表达式

如果布尔表达式为真,assert 语句不做任何事情;如果布尔表达式为假,assert 语句抛出 AssertionError 类型的异常。

编写一个程序 AssertionError.py,功能是计算列表前 n 项元素之和:

def calcSum(list, n):
    assert n <= len(list)

    sum = 0
    for i in range(n):
        sum += list[i]
    print('sum = %d' % sum)

list = [11, 22, 33, 44]    
calcSum(list, 3)
calcSum(list, 5)
  • 在第 1 行,calcSum 计算列表 list 的前 n 项之和
  • 在第 2 行,使用 assert 语句验证参数 n 是否小于等于 list 的长度
    • 正常情况下,n 是小于等于 list 的长度
    • 如果 n 大于 list 的长度,则表示输入参数 n 有错误
  • 在第 9 行,创建一个长度为 4 的列表
    • 在第 10 行,传递参数 n 等于 3,是一个合法的参数
    • 在第 11 行,传递参数 n 等于 5,是一个非法的参数

程序输出结果如下:

sum = 66
Traceback (most recent call last):
  File "AssertionError.py", line 11, in <module>
    calcSum(list, 5)
  File "AssertionError.py", line 2, in calcSum
    assert n <= len(list)
AssertionError
  • 在第 1 行,输出 sum = 66
    • calc(sum, 3) 计算列表前 3 项
    • 结果为 66
  • 在第 7 行,输出 AssertionError
    • calc(sum, 5) 计算列表前 5 项
    • 列表只有 4 项元素
    • 产生 AssertionError 类型的异常

3. AttributeError 的出现场景

Python 使用 object.property 的形式访问对象的属性,如果没有定义指定名称的属性,则会抛出 AttributeError 类型的异常。

编写程序 AttributeError.py,程序定义了类 Person,Person 包含有两个属性:name 和 age,代码如下:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

tom = Person('tom', 10)
print(tom.name)
print(tom.age)
print(tom.address)     
  • 在第 1 行,定义类 Person,Person 包含有两个属性:name 和 age;
  • 在第 6 行,实例化创建一个对象 tom;
    • 属性 name 为 ‘tom’;
    • 属性 age 为 10;
  • 在第 7 行,访问属性 name ;
  • 在第 8 行,访问属性 age;
  • 在第 9 行,访问属性 address,在类 Person 中没有定义该属性

程序输出结果如下:

tom
10
Traceback (most recent call last):
  File "AttributeError.py", line 9, in <module>
    print(tom.address)
AttributeError: 'Person' object has no attribute 'address'
  • 在第 1 行,输出属性 name 的值;
  • 在第 2 行,输出属性 age 的值;
  • 在第 1 行,属性 address 不存在,产生 AttributeError 类型的异常。

4. FileNotFoundError 的出现场景

python 使用函数 open(path) 打开指定路径的文件,如果文件不存在,则产生 FileNotFoundError 类型的异常,示例如下:

>> open('non-exist-file')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'non-exist-file'
  • 在第 4 行,因为文件 non-exist-file 不存在,产生 FileNotFoundError 类型的异常。

5. ModuleNotFoundError 的出现场景

python 使用关键字 import module_name 打开导入名称的模块,如果模块不存在,则产生 ModuleNotFoundError 类型的异常,示例如下:

>>> import non_exist_module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'non_exist_module'
  • 在第 4 行,因为模块 non_exist_file 不存在,产生 ModuleNotFoundError 类型的异常

6. IndexError 的出现场景

在 Python 使用 list[index] 的形式访问列表 list 的指定位置的元素,要求 index:

  • 大于等于 0
  • 小于列表的长度

如果 index 不在合法范围,则产生 IndexError 类型的异常。

>>> list = ['www', 'imooc', 'com']
>>> list[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
  • 在第 1 行,创建长度为 3 的列表;
    • 合法的 index 是 0、1、2;
  • 在第 2 行,index 不在合法范围;
    • 在第 5 行,产生 IndexError 类型的异常。

7. NameError 的出现场景

Python 在读取变量时,要求变量必须已经定义。如果读取一个尚未定义的变量,会产生 NameError 类型的异常。

>>> variable = 123
>>> print(varible)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'varible' is not defined
  • 在第 1 行,创建变量 variable;
  • 在第 2 行,此处将 variable 错误的拼写成 varible;
    • 变量 varible 还没有创建;
    • 在第 5 行,产生 NameError 类型的异常。

8. SyntaxError 的出现场景

Python 程序中出现语法错误时,会产生 SyntaxError 类型的异常。编写程序 SyntaxError.py

if 2>1
    print('2>1 is True')
    print('2>1 is False')
  • 在第 1 行,有一处语法错误,在行尾缺少冒号 :

程序输出结果如下:

  File "SyntaxError.py", line 1
    if 2>1
         ^
SyntaxError: invalid syntax
  • 在第 1 行,File “SyntaxError.py”, line 1
  • 在第 4 行,产生 SyntaxError 类型的异常

9. IndentationError 的出现场景

Python 程序中出现缩进的语法错误时,会产生 IndentationError 类型的异常。编写程序 IndentationError.py

if 2>1:
    print('2>1 is True')
  print('2>1 is False')
  • 在第 2 行,缩进为 4 个空格
  • 在第 3 行,缩进为 2 个空格

程序输出结果如下:

  File "IndentationError.py", line 3
    print('2>1 is False')
                        ^
IndentationError: unindent does not match any outer indentation level
  • 在第 4 行,输出 IndentationError;
    • 源程序第 2 行的缩进为 2 个空格;
    • 源程序第 3 行的缩进为 4 个空格;
    • 两者不匹配,产生 IndentationError 类型的异常。

11. Python 的标准异常类型总结

在上面的小节中讲解了常见的异常类型,Python 中全部的标准的异常类型如下:

异常名称 描述
SystemExit 解释器请求退出
KeyboardInterrupt 用户中断执行(通常是输入^C)
Exception 常规错误的基类
StopIteration 迭代器没有更多的值
GeneratorExit 生成器(generator)发生异常来通知退出
StandardError 所有的内建标准异常的基类
ArithmeticError 所有数值计算错误的基类
FloatingPointError 浮点计算错误
OverflowError 数值运算超出最大限制
ZeroDivisionError 除(或取模)零
AssertionError 断言语句失败
AttributeError 对象没有这个属性
EOFError 没有内建输入,到达EOF
EnvironmentError 操作系统错误的基类
IOError 输入/输出操作失败
OSError 操作系统错误
WindowsError 系统调用失败
ImportError 导入模块/对象失败
LookupError 无效数据查询的基类
IndexError 序列中没有此索引(index)
KeyError 映射中没有这个键
MemoryError 内存溢出错误(对于Python
NameError 未声明/初始化对象
UnboundLocalError 访问未初始化的本地变量
ReferenceError 弱引用(Weak
RuntimeError 一般的运行时错误
NotImplementedError 尚未实现的方法
SyntaxError Python
IndentationError 缩进错误
TabError Tab
SystemError 一般的解释器系统错误
TypeError 对类型无效的操作
ValueError 传入无效的参数
UnicodeError Unicode
UnicodeDecodeError Unicode
UnicodeEncodeError Unicode
UnicodeTranslateError Unicode
DeprecationWarning 关于被弃用的特征的警告
FutureWarning 关于构造将来语义会有改变的警告
OverflowWarning 旧的关于自动提升为长整型(long)的警告
PendingDeprecationWarning 关于特性将会被废弃的警告
RuntimeWarning 可疑的运行时行为(runtime
SyntaxWarning 可疑的语法的警告
UserWarning 用户代码生成的警告