鱼C论坛

 找回密码
 立即注册
查看: 2042|回复: 0

[技术交流] 032讲总结

[复制链接]
发表于 2017-8-23 23:13:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
exception 异常
Python异常总结[ 扩展阅读 ](只举常用)

AssertionError 断言语句(assert)失败,如:
>>> my_list = ['小甲鱼是帅哥']
>>> assert len(my_list) > 0
>>> my_list.pop()
'小甲鱼是帅哥'
>>> assert len(my_list) > 0
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    assert len(my_list) > 0
AssertionError

AttributeError 尝试访问未知的对象属性,如:
>>> my_list.fishc
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    my_list.fishc
AttributeError: 'list' object has no attribute 'fishc'

IndexError 索引超出范围,如:
>>> my_list = [1, 2, 3]
>>> my_list[3]
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    my_list[3]
IndexError: list index out of range

KeyError 字典中查找一个不存在的关键字,如:
>>> my_dict = {'one':1, 'two':2, 'three':3}
>>> my_dict['one']
1
>>> my_dict['four']
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    my_dict['four']
KeyError: 'four'

NameError 尝试访问一个不存在的变量,如:
>>> fishc
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    fishc
NameError: name 'fishc' is not defined

OSError 操作系统产生的异常(例如打开一个不存在的文件),如:
请输入需要打开的文件名:readme
Traceback (most recent call last):
  File "F:/python教程练习/练习32.1.py", line 2, in <module>
    f = open(file_name)
FileNotFoundError: [Errno 2] No such file or directory: 'readme'

OverflowError 数值运算超过最大限制(一般不会出现)

SyntaxError Python的语法错误,如:
>>> print 'I love FishC.com'
SyntaxError: invalid syntax

TypeError 不同类型间的无效操作,如:
>>> 1 + '1'
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    1 + '1'
TypeError: unsupported operand type(s) for +: 'int' and 'str'

ZeroDivisionError 除数为零,如:
>>> 5 / 0
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    5 / 0
ZeroDivisionError: division by zero

评分

参与人数 1鱼币 +1 收起 理由
小甲鱼 + 1

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 22:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表