-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexception.py
58 lines (49 loc) · 1.28 KB
/
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
#!/usr/bin/env python3
'''
This file is to learn about exception more detail.
'''
# test exception cases
class TestCase():
# case1 ZeroDivisionError: division by zero
def two_number_divided(self,a,b):
s = a / b
return s
# case2 FileNotFoundError: [Errno 2] No such file or directory: 'a'
def open_a_none_file(self,name):
f = open(name,'r')
f.close()
# case3 handle execption
def handle_exception(self,a,b):
try:
result = self.two_number_divided(a,b)
except ZeroDivisionError as e:
print('exception message:',e)
else:
print(result)
finally:
pass
class TestCase2():
# case4 throw exception
def throw_exception(self,a):
if a == 0:
raise ValueError('a is zero!')
# case5 define self exception
class myException(ValueError):
pass
def show_self_exception():
raise myException('hhhhh')
if __name__ == '__main__':
test = TestCase()
# case1
# test.two_number_divided(1,0)
# case2
# test.open_a_none_file('a')
# case3
test.handle_exception(5,2)
test.handle_exception(5,0)
#######
test2 = TestCase2()
# case4
# test2.throw_exception(0)
# case5
# show_self_exception()