-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathEHFinally.py
38 lines (26 loc) · 1.13 KB
/
EHFinally.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
# Important point to be noted.
# finally block is always executed after leaving the try statement.
# In case if some exception was not handled by except block, it is re-raised after execution of finally block.
# finally block is used to deallocate the system resources.
# One can use finally just after try without using except block, but no exception is handled in that case.
# here's the sample exercise for understanding `finally` keyword in python
try:
inFromUser = int(input("Hello user, give me a integer input: "))
k = 5//inFromUser # raises divide by zero exception.
print(k)
# handles zerodivision exception
except ZeroDivisionError:
print("Can't divide by zero")
except TypeError:
print("Given in put is not in a right datatype")
finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')
for num in list(range(0,10)):
if num%2 ==0:
continue
print(num)
# have you heard about break/continue
# by default continue will not support inside finally
# from 3.8.3, continue is supported inside finally