-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcept.py
More file actions
62 lines (51 loc) · 1.99 KB
/
Except.py
File metadata and controls
62 lines (51 loc) · 1.99 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
import inspect
print(' 1. Handling single exceptions with try-except is in this form')
try:
file = open('C:/Users/Anwender/PycharmProjects/PythonInterm/Test', 'rb')
except IOError as e:
print('An IOError occurred. {}'.format(e.args[-1]))
print('to handle multiple exceptions')
print('1. possibility with a tuple and multiple except inside')
try:
file = open('C:/Users/Anwender/PycharmProjects/PythonInterm/Test', 'rb')
except (IOError, EOFError) as e:
print("An error occurred. {}".format(e.args[-1]))
print('2. multiple except blocks')
try:
file = open('C:/Users/Anwender/PycharmProjects/PythonInterm/Test', 'rb')
except EOFError as e:
print("An EOF error occurred.")
raise e
except IOError as e:
print("An error occurred.")
raise e
print('3. capture all general except when no idea of spec')
try:
file = open('C:/Users/Anwender/PycharmProjects/PythonInterm/Test', 'rb')
except Exception:
# Some logging if you want
raise
print('The use of finally is for cleanup after except was or wasnt caught')
try:
file = open('C:/Users/Anwender/PycharmProjects/PythonInterm/Test', 'rb')
except IOError as e:
print('An IOError occurred. {}'.format(e.args[-1]))
finally:
print("This would be printed whether or not an exception occurred!")
# Output: An IOError occurred. No such file or directory
# This would be printed whether or not an exception occurred!
print('try-else. Else includes if no exception occurs, here no Error is caught ')
try:
print('I am sure no exception is going to occur!')
except Exception:
print('exception')
else:
# any code that should only run if no exception occurs in the try,
# but for which exceptions should NOT be caught
print('This would only run if no exception occurs. And an error here '
'would NOT be caught.')
finally:
print('This would be printed in every case.')
# Output: I am sure no exception is going to occur!
# This would only run if no exception occurs.
# This would be printed in every case.