鱼C论坛

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

[技术交流] Python--032异常处理:你不可能总是对的

[复制链接]
发表于 2017-7-17 23:59:03 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 游戏小虾米 于 2017-7-19 23:17 编辑

Tip:
异常可以告诉哪里出了什么错

一,理论
http://bbs.fishc.com/thread-45814-1-2.html
Python标准异常总结

二,应用
1
>>> list = []
>>> assert len(list) > 0
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    assert len(list) > 0
AssertionError

2
>>> list.fishC()
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    list.fishC()
AttributeError: 'list' object has no attribute 'fishC'

3
>>> list[1]
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    list[1]
IndexError: list index out of range

4
>>> my_dict = {'one':1, 'six one eight':618}
>>> my_dict['eleven eleven']
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    my_dict['eleven eleven']
KeyError: 'eleven eleven'

5
>>> fishC
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    fishC
NameError: name 'fishC' is not defined

6
>>> f = open('F:\\readme.txt')
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    f = open('F:\\readme.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'F:\\readme.txt'

7
>>> print 'cangjingkong'
SyntaxError: Missing parentheses in call to 'print'

8
>>> 1 + '1'
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    1 + '1'
TypeError: unsupported operand type(s) for +: 'int' and 'str'

>>> 5 / 0
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    5 / 0
ZeroDivisionError: division by zero


三,课后练习

评分

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

查看全部评分

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 20:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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