Python中常见的异常
1、除数为零异常(ZeroDivisionError)
# coding:utf-8
print(10/0)
- 1
- 2
2、索引无效异常(IndexError)
# coding:utf-8
lis = [1, 2, 3, 4]
print(lis[4])
- 1
- 2
- 3
3、数据类型不匹配异常(TypeError)
# coding:utf-8
print(1 > 'hj')
- 1
- 2
4、键值无效异常(KeyError)
# coding:utf-8
dic = {"code":1001, "name":"zhang"}
print(dic["key"])
- 1
- 2
- 3
5、语法异常(SyntaxError)
# coding:utf-8
int a = 1
- 1
- 2
6、参数值异常(ValueError)
# coding:utf-8
a = int('hj')
- 1
- 2