forked from jackzhenguo/python-small-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcepter.py
More file actions
45 lines (32 loc) · 704 Bytes
/
excepter.py
File metadata and controls
45 lines (32 loc) · 704 Bytes
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
import time
import math
def excepter(f):
i = 0
t1 = time.time()
def wrapper():
try:
f()
except Exception as e:
nonlocal i
i += 1
print(f'{e.args[0]}: {i}')
t2 = time.time()
if i == n:
print(f'spending time:{round(t2-t1,2)}')
return wrapper
n = 10 # except count
@excepter
def divide_zero_except():
time.sleep(0.1)
j = 1/(40-20*2)
# test zero divived except
for _ in range(n):
divide_zero_except()
@excepter
def outof_range_except():
a = [1, 3, 5]
time.sleep(0.1)
print(a[3])
# test out of range except
for _ in range(n):
outof_range_except()