|
发表于 2017-7-31 21:09:40
|
显示全部楼层
0.
捕获异常才能避免让用户看到程序出错,提高程序的友好性.
1.
>>> my_list = [1, 2, 3, 4,,]
ValueError
2.
>>> my_list = [1, 2, 3, 4, 5]
>>> print(my_list[len(my_list)])
IndexError
3.
>>> my_list = [3, 5, 1, 4, 2]
>>> my_list.sorted()
AttributeError
4.
>>> my_dict = {'host': 'http://bbs.fishc.com', 'port': '80'}
>>> print(my_dict['server'])
ValueError
5.
def my_fun(x, y):
print(x, y)
my_fun(x=1, 2)
6.
f = open('C:\\test.txt', wb)
f.write('I love FishC.com!\n')
f.close()
SyntaxError
7.
def my_fun1():
x = 5
def my_fun2():
x *= x
return x
return my_fun2()
my_fun1()
NameError |
|