-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptions.py
112 lines (98 loc) · 2.91 KB
/
exceptions.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# EXCEPTIONS
# More info: https://docs.python.org/3/tutorial/errors.html
# Advice: Only concentrate on the exceptions
def sequence_of_the_exceptions():
"""
If an exception occurs during the execution of the try clause,
the exception can be handled by an except clause.
If the except clause does not handle the exception,
the exception is rebuilt after the finally clause has been executed.
"""
try:
raise NameError("Hi error")
except SyntaxError:
print("Error SyntaxError")
finally:
print("Finished")
def behavior_with_break_continue_statements():
"""
If the try statement reaches a break, continue or return communicated,
the finally clause will be executed just before the break,
continue return the declaration execution.
"""
n = 0
while n < 10:
try:
print(n)
if n == 4:
break
print("Bye")
except Exception:
print("Error")
finally:
print("finished")
n += 1
def behavior_of_return_in_try_finally():
"""
If a finally clause includes a return declaration,
the returned value will be the one of
the declaration of the finally clause return,
not the value of the declaration of the try clause return.
"""
try:
return True
finally:
return False
# CORRECT USE OF try, except, else, finally keywords
# syntax: try ... except ... else ... finally
# example 1
def through_list(list_):
"""
Evaluate if is valid convert each values of the list
to integer
"""
for k in list_:
try:
i = int(k)
except ValueError:
print("Error")
else:
print("Yep")
finally:
print("Completed process\n"+"="*30)
# example 2
def divide(x, y):
"""
Evaluate if is valid divide 'x' value by 'y' value
"""
try:
r = x/y
except ZeroDivisionError:
print('Not divide by zero')
except TypeError:
print('Not input an string')
else:
print(f'The result is: {round(r, 2)}')
finally:
print('Completed process\n' + '='*30)
# USING raise
# example 1
def concat_by(string_):
"""
Concat string_ value. If this value is not a string
print and message error
"""
if not isinstance(string_, str):
raise ValueError('Only strings')
return 'Your message is: ' + string_
if __name__ == '__main__':
# sequence_of_the_exceptions()
# behavior_with_break_continue_statements()
through_list([1.0, 'two', 3]) # Yep Error Yep
divide(2, 3) # the result is: 0.67
divide(2, 0) # Not divide by zero
divide('2', '3') # Not input an string
divide(10, 2) # the result is: 5.0
print(concat_by('Through the eyes')) # Your message is: Though the eyes
# print(concat_by(32)) # Only strings
print(behavior_of_return_in_try_finally()) # return False