Skip to content

Commit d1be0af

Browse files
author
Scott Archer-Nicholls
committed
feat: better try/except/else case
1 parent 2f2b7d0 commit d1be0af

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

episodes/05-defensive_programming.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ Please look at the following code. Can you find the fundamental problem in this
3434
val = 1
3535

3636
if val > 0:
37-
print('Value: ', val, 'is positive.')
37+
print('Value:', val, 'is positive.')
3838
elif val == 0:
39-
print('Value: ', val, 'is zero.')
39+
print('Value:', val, 'is zero.')
4040
else:
41-
print('Value: ', val, 'is negative.')
41+
print('Value:', val, 'is negative.')
4242
```
4343

4444
::::::::::::::: solution
@@ -51,11 +51,11 @@ The test assumes that `val` is a number, and throws an uncontrolled error if it
5151
val = 'a'
5252

5353
if val > 0:
54-
print('Value: ', val, 'is positive.')
54+
print('Value:', val, 'is positive.')
5555
elif val == 0:
56-
print('Value: ', val, 'is zero.')
56+
print('Value:', val, 'is zero.')
5757
else:
58-
print('Value: ', val, 'is negative.')
58+
print('Value:', val, 'is negative.')
5959
```
6060

6161
```output
@@ -64,9 +64,9 @@ TypeError Traceback (most recent call last)
6464
<ipython-input-2-99c0e25bf5e9> in <module>()
6565
1 def check_sign(val):
6666
----> 2 if val > 0:
67-
3 print('Value: ', val, 'is positive.')
67+
3 print('Value:', val, 'is positive.')
6868
4 elif val == 0:
69-
5 print('Value: ', val, 'is zero.')
69+
5 print('Value:', val, 'is zero.')
7070
7171
TypeError: '>' not supported between instances of 'str' and 'int'
7272
```
@@ -81,11 +81,11 @@ To make things simpler, we will first write the test as a function:
8181
```python
8282
def check_sign(val):
8383
if val > 0:
84-
print('Value: ', val, 'is positive.')
84+
print('Value:', val, 'is positive.')
8585
elif val == 0:
86-
print('Value: ', val, 'is zero.')
86+
print('Value:', val, 'is zero.')
8787
else:
88-
print('Value: ', val, 'is negative.')
88+
print('Value:', val, 'is negative.')
8989
```
9090

9191
Then wrap the function call in an `if` statement:
@@ -131,15 +131,19 @@ As with `if` statements, multiple `except` statements can be used, each with a d
131131
```python
132132
try:
133133
check_sign(val)
134+
reciprocal = 1/val
134135
except TypeError as err:
135136
print('Val is not a number')
136137
print('But our code does not crash anymore')
137138
print('The run-time error is:', err)
139+
except Exception as err:
140+
print('Some error other than a TypeError occured')
141+
print('But our code does not crash')
142+
print('The run-time error is:', err)
138143
else:
139-
print('The value provided to the check_sign function did not result in a TypeError')
144+
print('The reciprocal of the value =', reciprocal)
140145
finally:
141146
print('release memory')
142-
del(val)
143147
```
144148

145149
The typical use of the `finally` statement is to deal with the release of external resources (such as files or network connections) whether or not the attempted action has been successful.

0 commit comments

Comments
 (0)