Python 错误和异常

Python 错误和异常

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

  • 如果程序中有语法错误,会产生 SyntaxError 类型的异常

  • 执行除以 0 的运算,会产生 ZeroDivisionError 类型的异常

  • 打开一个不存在的文件,会产生 IOError 类型的异常

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

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

1. ZeroDivisionError 的出现场景

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

>>>  / Traceback (most recent call last):
  File <stdin>, line , 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 = for i in range(n):sum += list[i]print('sum = %d' % sum)list = [, , , ]    calcSum(list, )calcSum(list, )
  • 在第 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', )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 , in <module>FileNotFoundError: [Errno ] 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 , 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[]Traceback (most recent call last):
  File <stdin>, line , in <module>IndexError: list index out of range
  • 在第 1 行,创建长度为 3 的列表;

    • 合法的 index 是 0、1、2;

  • 在第 2 行,index 不在合法范围;

    • 在第 5 行,产生 IndexError 类型的异常。

7. NameError 的出现场景

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

>>> variable = >>> print(varible)Traceback (most recent call last):
  File <stdin>, line , in <module>NameError: name 'varible' is not defined
  • 在第 1 行,创建变量 variable;

  • 在第 2 行,此处将 variable 错误的拼写成 varible;

    • 变量 varible 还没有创建;

    • 在第 5 行,产生 NameError 类型的异常。

8. SyntaxError 的出现场景

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

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

程序输出结果如下:

  File SyntaxError.py, line 1if 2>1
         ^
SyntaxError: invalid syntax
  • 在第 1 行,File “SyntaxError.py”, line 1

  • 在第 4 行,产生 SyntaxError 类型的异常

9. IndentationError 的出现场景

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

if >: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尚未实现的方法
SyntaxErrorPython
IndentationError缩进错误
TabErrorTab
SystemError一般的解释器系统错误
TypeError对类型无效的操作
ValueError传入无效的参数
UnicodeErrorUnicode
UnicodeDecodeErrorUnicode
UnicodeEncodeErrorUnicode
UnicodeTranslateErrorUnicode
DeprecationWarning关于被弃用的特征的警告
FutureWarning关于构造将来语义会有改变的警告
OverflowWarning旧的关于自动提升为长整型(long)的警告
PendingDeprecationWarning关于特性将会被废弃的警告
RuntimeWarning可疑的运行时行为(runtime
SyntaxWarning可疑的语法的警告
UserWarning用户代码生成的警告