-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path23_1_trycatch.py
143 lines (115 loc) · 2.93 KB
/
23_1_trycatch.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# %% [markdown] try catch
"""
Finally execution
catching exceptions
with statement intro
"""
# %%
def example1():
try:
print(undefined_var)
finally:
print("Executed in the finally block")
example1()
# %%
def example2():
try:
print("defined")
finally:
print("Executed in the finally block")
example2()
# %%
def example3():
try:
print(undefined_var)
except:
print("The error was caught")
finally:
print("Executed in the finally block")
example3()
# %%
def example4():
try:
print("defined")
except:
print("The error was caught")
finally:
print("Executed in the finally block")
example4()
# * finally is executed after the try except statement. If there is an error, it first goes through the except statement and then in the finally block. If there is no except statement or if the error is raised again in the except, the code in finally is executed BEFORE the error is raised
# %%
# Bonus catch the right error
def example5():
try:
print(undefined_var)
except NameError:
print("The error was caught")
example5()
# %%
def example6():
try:
print(undefined_var)
except ValueError:
print("The error was caught")
# Bonus raise after catch : execute code only if there is an error (not the same as finally : why ? Because finally executes whether or not the try statement is successful or not
example6()
# %%
def example7():
try:
print(undefined_var)
except:
print("The error was caught and we will raise it again")
raise
example7()
# %% [markdown]
# # :Bonus the with statement
# %%
def example8():
with open("jambon.txt", "w") as f:
f.write("beurre")
def example8bis():
f = open("jambon.txt", "w")
f.write("beurre")
f.close()
# ? example8 and example8bis : do they really do the same thing ?
# ? What if we change w with r
def example9():
with open("jambon.txt", "r") as f:
f.write("beurre")
def example9bis():
f = open("jambon.txt", "r")
f.write("beurre")
f.close()
# %%
example8()
# %%
example8bis()
# %%
example9()
# %%
example9bis()
# %%
# * You guessed it the f.close is not executed in example9bis. In fact if i want to achieve the same result as example8, i should write:
# * This guarantees the file is closed even if an error occurs.
def example8ter():
f = open("jambon.txt", "w")
try:
f.write("beurre")
finally:
f.close()
# example1()
# There was an error but the code in the finally block was executed nonetheless. The error was then propagated
# example2()
# No error : code in the finally block was executed
# example3()
# Error caught : the programm does not crash. Finally block is executed
# example4()
# No error, so it is the same as example2
# example5()
# Error was caught
# example6()
# Error was not caught
# example7()
# Error was caught and raised again
# example8()
# %%