# Python – Try Except Finally

```a = 10
b = 1
try:
a / b
except ZeroDivisionError:
print('division by 0')
finally:
print('this always executes')```
`this always executes`

```a = 10
b = 0
try:
a / b
except ZeroDivisionError:
print('division by 0')
finally:
print('this always executes')```
```division by 0
this always executes```

```a = 0
b = 2

while a < 3:
print('-------------')
a += 1
b -= 1
try:
res = a / b
except ZeroDivisionError:
print('{0}, {1} - division by 0'.format(a, b))
res = 0
continue
finally:
print('{0}, {1} - always executes'.format(a, b))

print('{0}, {1} - main loop'.format(a, b))```
```-------------
1, 1 - always executes
1, 1 - main loop
-------------
2, 0 - division by 0
2, 0 - always executes
-------------
3, -1 - always executes
3, -1 - main loop```

```a = 0
b = 2

while a < 3:
print('-------------')
a += 1
b -= 1
try:
res = a / b
except ZeroDivisionError:
print('{0}, {1} - division by 0'.format(a, b))
res = 0
break
finally:
print('{0}, {1} - always executes'.format(a, b))

print('{0}, {1} - main loop'.format(a, b))```
```-------------
1, 1 - always executes
1, 1 - main loop
-------------
2, 0 - division by 0
2, 0 - always executes```

```a = 0
b = 2

while a < 3:
print('-------------')
a += 1
b -= 1
try:
res = a / b
except ZeroDivisionError:
print('{0}, {1} - division by 0'.format(a, b))
res = 0
break
finally:
print('{0}, {1} - always executes'.format(a, b))

print('{0}, {1} - main loop'.format(a, b))
else:
print('\n\nno errors were encountered!')```
```-------------
1, 1 - always executes
1, 1 - main loop
-------------
2, 0 - division by 0
2, 0 - always executes```

```a = 0
b = 5

while a < 3:
print('-------------')
a += 1
b -= 1
try:
res = a / b
except ZeroDivisionError:
print('{0}, {1} - division by 0'.format(a, b))
res = 0
break
finally:
print('{0}, {1} - always executes'.format(a, b))

print('{0}, {1} - main loop'.format(a, b))
else:
print('\n\nno errors were encountered!')```
```-------------
1, 4 - always executes
1, 4 - main loop
-------------
2, 3 - always executes
2, 3 - main loop
-------------
3, 2 - always executes
3, 2 - main loop

no errors were encountered!```