-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandling_in_Python.py
More file actions
94 lines (69 loc) · 1.65 KB
/
ErrorHandling_in_Python.py
File metadata and controls
94 lines (69 loc) · 1.65 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
str_to_float = ['2.1', '2.3', '7,5', '$12.12', '8.9', '5%', '33.1', '33#1']
floats = []
for s in str_to_float:
try:
floats.append(float(s))
except:
floats.append(None)
print('exception')
print (floats)
def adder(x, y):
try:
z = x+y
except TypeError as target:
return ('cannot add these types together')
else:
return ('result is:', z)
print (adder(5,7))
print (adder(5,"7"))
def no_four(number):
if number == 4:
raise ValueError('No fours allowed!!')
else:
print('Number:', number)
no_four(2)
print ("===========Knowledge Check===========")
def compare(a, b):
try:
a_greater = (a > b)
except:
a_greater = None
b_greater = None
else:
b_greater = (b > a)
finally:
print(a_greater, b_greater)
compare(10,11)
print ("===========Knowledge Check===========")
def positive_only(a, b):
output = a + b
if output >= 0:
return output
else:
# Your code here. to throw an valueError
raise ValueError
#positive_only(10,-25)
print ("===========Knowledge Check===========")
def int_checker(a, b):
try:
if isinstance(a, int) and isinstance(b, int):
print('both integers')
else:
raise ValueError
except:
print('error occurred')
else:
print('no error occurred!')
finally:
print('function completed')
int_checker(5,6)
int_checker('5',6)
print ("===========Knowledge Check===========")
def divider(a, b):
try:
c = a/b
# else:
# print('divided:', c)
except:
print('could not divide numbers')
divider(10, 2)