-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP-Exceptions.py
More file actions
427 lines (346 loc) · 13.3 KB
/
OOP-Exceptions.py
File metadata and controls
427 lines (346 loc) · 13.3 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
########## EXCEPTIONS in OOP context ##########
### Exceptions are Classes
# when an exception is raised, an object of the class is instantiated and goes through all levels of program execution,
# looking for the except branch that is prepared to deal with it
def print_exception_tree(thisclass, nest = 0):
if nest > 1:
print(" |" * (nest - 1), end="")
if nest > 0:
print(" +---", end="")
print(thisclass.__name__)
for subclass in thisclass.__subclasses__():
print_exception_tree(subclass, nest + 1)
print_exception_tree(BaseException)
# BaseException
# +---BaseExceptionGroup
# | +---ExceptionGroup
# +---Exception
# | +---ArithmeticError
# | | +---FloatingPointError
# | | +---OverflowError
# | | +---ZeroDivisionError
# | +---AssertionError
# | +---AttributeError
# | +---BufferError
# | +---EOFError
# | +---ImportError
# | | +---ModuleNotFoundError
# | | +---ZipImportError
# | +---LookupError
# | | +---IndexError
# | | +---KeyError
# | | +---CodecRegistryError
# | +---MemoryError
# | +---NameError
# | | +---UnboundLocalError
# | +---OSError
# | | +---BlockingIOError
# | | +---ChildProcessError
# | | +---ConnectionError
# | | | +---BrokenPipeError
# | | | +---ConnectionAbortedError
# | | | +---ConnectionRefusedError
# | | | +---ConnectionResetError
# | | +---FileExistsError
# | | +---FileNotFoundError
# | | +---InterruptedError
# | | +---IsADirectoryError
# | | +---NotADirectoryError
# | | +---PermissionError
# | | +---ProcessLookupError
# | | +---TimeoutError
# | | +---UnsupportedOperation
# | +---ReferenceError
# | +---RuntimeError
# | | +---NotImplementedError
# | | +---PythonFinalizationError
# | | +---RecursionError
# | | +---_DeadlockError
# | +---StopAsyncIteration
# | +---StopIteration
# | +---SyntaxError
# | | +---IndentationError
# | | | +---TabError
# | | +---_IncompleteInputError
# | +---SystemError
# | | +---CodecRegistryError
# | +---TypeError
# | +---ValueError
# | | +---UnicodeError
# | | | +---UnicodeDecodeError
# | | | +---UnicodeEncodeError
# | | | +---UnicodeTranslateError
# | | +---NotShareableError
# | | +---UnsupportedOperation
# | +---Warning
# | | +---BytesWarning
# | | +---DeprecationWarning
# | | +---EncodingWarning
# | | +---FutureWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---RuntimeWarning
# | | +---SyntaxWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ImportWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---RuntimeWarning
# | | +---SyntaxWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---RuntimeWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---RuntimeWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---RuntimeWarning
# | | +---SyntaxWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ImportWarning
# | | +---ImportWarning
# | | +---PendingDeprecationWarning
# | | +---ResourceWarning
# | | +---RuntimeWarning
# | | +---SyntaxWarning
# | | +---UnicodeWarning
# | | +---UserWarning
# | +---InterpreterError
# | | +---InterpreterNotFoundError
# | +---ExceptionGroup
# +---GeneratorExit
# +---KeyboardInterrupt
# +---SystemExit
### .ARGS
# la méthode .args est un attribut des objets d'exception (instances de classes dérivées de BaseException, comme Exception).
# Si l'exception est levée sans argument (raise Exception()), alors e.args est une liste vide ([]).
# Si l'exception est levée avec un seul argument (raise Exception("message")), alors e.args est une tuple avec un seul élément (("message",)).
# Si plusieurs arguments sont donnés (raise Exception("msg1", "msg2")), alors e.args est une tuple contenant tous ces arguments (("msg1", "msg2")).
def print_arguments(arguments):
lng = len(arguments)
if lng == 0:
print("")
elif lng == 1:
print(arguments[0])
else:
print(str(arguments))
try:
raise Exception # La ligne raise Exception génère une exception de type Exception sans message.
except Exception as e: # L'exception est attrapée dans le bloc except sous la variable e
print(e, e.__str__(), sep=' : ' ,end=' : ')
print_arguments(e.args) # = []
# : :
try:
raise Exception("my exception")
except Exception as e:
print(e, e.__str__(), sep=' : ', end=' : ')
print_arguments(e.args) # = (my exception) tupple
# my exception : my exception : my exception
try:
raise Exception("my", "exception")
except Exception as e:
print(e, e.__str__(), sep=' : ', end=' : ')
print_arguments(e.args) # = ("my", "exception") tupple
# ('my', 'exception') : ('my', 'exception') : ('my', 'exception')
try:
raise Exception(12345)
except Exception as e:
print(e, e.__str__(), sep=' : ', end=' : ')
print_arguments(e.args) # = ("12345") tupple, l'int 12345 a bien été chagée en str
# 12345 : 12345 : 12345
### Exemple intéressant
# class + Error + boucle for
class I:
def __init__(self):
self.s = 'abc'
self.i = 0
def __iter__(self):
return self
def __next__(self):
if self.i == len(self.s):
raise StopIteration
v = self.s[self.i]
self.i += 1
return v
for x in I(): # va executer chaque method de la fonction
print(x,end='') # print le return final snas aller à la ligne, sans espace
# abc
# Étape | Action | Résultat / Variable | Affichage |
# |---------|------------------------------------------|--------------------------------------------------------|----------------------|
# | 1 | Crée instance `I()` | `self.i=0`, `self.s='abc'` | |
# | 2 | Appel `__iter__()` | Retourne l'objet lui-même | |
# | 3 | Appel `__next__()` | `i=0`, retourne `'a'`, `i=1` | Affiche `'a'` |
# | 4 | Appel `__next__()` | `i=1`, retourne `'b'`, `i=2` | Affiche `'b'` |
# | 5 | Appel `__next__()` | `i=2`, retourne `'c'`, `i=3` | Affiche `'c'` |
# | 6 | Appel `__next__()` | `i=3`, levée `StopIteration` | Fin de la boucle |
# raise StopIteration est la façon officielle en Python de dire :
# ==> Fini, il n'y a plus d'éléments à fournir
# La boucle for ou tout autre code qui utilise l'itérateur va alors arrêter automatiquement la boucle
### CHAINED EXCEPTIONS
## CHAIN OF EXCEPTION
a_list = ['First error', 'Second error']
try:
print(a_list[3])
except Exception as e:
print(0 / 0)
# Traceback (most recent call last):
# File "c:\PythonLearning\SandBox.py", line 4, in <module>
# print(a_list[3])
# ~~~~~~^^^
# IndexError: list index out of range
# During handling of the above exception, another exception occurred: ==> CHAIN of exceptions
# Traceback (most recent call last):
# # File "c:\PythonLearning\SandBox.py", line 6, in <module>
# print(0 / 0)
# ~~^~~
# ZeroDivisionError: division by zero
## IMPLICIT Chaining & .__context__
a_list = ['First error', 'Second error']
try:
print(a_list[3])
except Exception as e:
try:
# the following line is a developer mistake - they wanted to print progress as 1/10 but wrote 1/0
print(1 / 0)
except ZeroDivisionError as f:
print('Inner exception (f):', f)
print('Outer exception (e):', e)
print('Outer exception referenced:', f.__context__) # linked to e
print('Is it the same object:', f.__context__ is e)
# Inner exception (f): division by zero
# Outer exception (e): list index out of range
# Outer exception referenced: list index out of range
# Is it the same object: True
# f.__context__ is e == True
# 1- IndexError is captured
# 2- e is associated to the current handled error = INdexError
# 3- ZeroDivisionError occurs, internal, because into the except block
# 4- Implicit chaining is created
# .__context__ refers to the exception being handled when a new one is raised during an except block
## EXPLICIT chaining
class RocketNotReadyError(Exception):
pass
def personnel_check():
try:
print("\tThe captain's name is", crew[0])
print("\tThe pilot's name is", crew[1])
print("\tThe mechanic's name is", crew[2])
print("\tThe navigator's name is", crew[3]) # will cause an exception as crew[3] doesn't exist
except IndexError as e:
raise RocketNotReadyError('Crew is incomplete') from e # explicit chaining
def fuel_check():
try:
print('Fuel tank is full in {}%'.format(100 / 2))
except ZeroDivisionError as e:
raise RocketNotReadyError('Problem with fuel gauge') from e
crew = ['John', 'Mary', 'Mike']
fuel = 100
check_list = [personnel_check, fuel_check]
print('Final check procedure')
# Final check procedure
for check in check_list:
try:
check()
except RocketNotReadyError as f:
print('RocketNotReady exception: "{}", caused by "{}"'.format(f, f.__cause__))
# The captain's name is John
# The pilot's name is Mary
# The mechanic's name is Mike
# RocketNotReady exception: "Crew is incomplete", caused by "list index out of range"
# Fuel tank is full in 50.0%
# .__raise__ refers to the direct cause of an exception when an explicit chaining is done with "raise"
## .__traceback__
# it is an attribute of each exception instance
class RocketNotReadyError(Exception):
pass
def personnel_check():
try:
print("\tThe captain's name is", crew[0])
print("\tThe pilot's name is", crew[1])
print("\tThe mechanic's name is", crew[2])
print("\tThe navigator's name is", crew[3])
except IndexError as e:
raise RocketNotReadyError('Crew is incomplete') from e
crew = ['John', 'Mary', 'Mike']
print('Final check procedure')
try:
personnel_check()
except RocketNotReadyError as f:
print(f.__traceback__)
print(type(f.__traceback__))
# Final check procedure
# The captain's name is John
# The pilot's name is Mary
# The mechanic's name is Mike
# <traceback object at 0x000001705F9F37C0>
# <class 'traceback'>
## traceback MODULE
# format_tb() method
# method delivered by the built-in traceback module to get a list of strings describing the traceback
# print_tb() method
# the same but to print strings directly to the standard output
import traceback
class RocketNotReadyError(Exception):
pass
def personnel_check():
try:
print("\tThe captain's name is", crew[0])
print("\tThe pilot's name is", crew[1])
print("\tThe mechanic's name is", crew[2])
print("\tThe navigator's name is", crew[3])
except IndexError as e:
raise RocketNotReadyError('Crew is incomplete') from e
crew = ['John', 'Mary', 'Mike']
print('Final check procedure')
# Final check procedure
try:
personnel_check()
except RocketNotReadyError as f:
print(f.__traceback__)
print(type(f.__traceback__))
print('\nTraceback details')
details = traceback.format_tb(f.__traceback__) # ==> format_tb()
print("\n".join(details))
# The captain's name is John
# The pilot's name is Mary
# The mechanic's name is Mike
# <traceback object at 0x00000217BD835680>
# <class 'traceback'>
# Traceback details
# File "c:\PythonLearning\SandBox.py", line 22, in <module>
# personnel_check()
# ~~~~~~~~~~~~~~~^^
# File "c:\PythonLearning\SandBox.py", line 14, in personnel_check
# raise RocketNotReadyError('Crew is incomplete') from e
print('Final check is over')
# Final check is over