-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_exception.py
58 lines (45 loc) · 1.22 KB
/
clean_exception.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-03-20 22:29:37
# @copyright by hoojo@2018
# @changelog Added python3 `exception -> clean exception` example
try:
raise KeyboardInterrupt
except:
print('exception!')
finally:
print('clean!')
def divide(x, y):
try:
result = x / y
except ZeroDivisionError as e:
print('zero error:', e)
else:
print('result:', result)
finally:
print('divide execute finish!')
divide(10, 1)
'''
result: 10.0
divide execute finish!
'''
divide(10, 0)
'''
zero error: division by zero
divide execute finish!
'''
divide('10', '2')
'''
error
'''
for line in open("myfile.txt"):
print(line, end="")
# 以上这段代码的问题是,当执行完毕后,文件会保持打开状态,并没有被关闭。
# 关键词 with 语句就可以保证诸如文件之类的对象在使用完之后一定会正确的执行他的清理方法:
with open("myfile.txt") as f:
for line in f:
print(line, end="")
# 以上这段代码执行完毕后,就算在处理过程中出问题了,文件 f 总是会关闭