|
263 | 263 | After the block, we print \pythonil{"The program is now finished."}.
|
264 | 264 | This code is executed only if no uncaught \pythonilIdx{Exception} has left the \pythonil{try}-\pythonil{except} block.
|
265 | 265 | The output of the program given in \cref{exec:exceptions:try_except_str_index} shows that this is indeed the case:
|
266 |
| -We first get the results of the successfull search for \pythonil{"Hello"}, followed by the output for the failed search. |
| 266 | +We first get the results of the successful search for \pythonil{"Hello"}, followed by the output for the failed search. |
267 | 267 | The last line then is \textil{The program is now finished.}
|
268 | 268 |
|
269 | 269 | \gitPythonAndErrorOutput{\programmingWithPythonCodeRepo}{06_exceptions}{try_multi_except.py}{--args format}{exceptions:try_multi_except}{%
|
270 |
| -The handling of multiple errors, namely \pythonilIdx{DivisionByZero} and \pythonilIdx{ArithmeticError}, as well as what happens if a variable remains unassigned due to an error (a \pythonilIdx{NameError} is raised).}% |
| 270 | +The handling of multiple errors, namely \pythonilIdx{ZeroDivisionError} and \pythonilIdx{ArithmeticError}, as well as what happens if a variable remains unassigned due to an error (a \pythonilIdx{NameError} is raised).}% |
271 | 271 | %
|
272 | 272 | In \cref{lst:exceptions:try_multi_except}, we revisit our new \pythonil{sqrt} function.
|
273 | 273 | This function will \pythonilIdx{raise} an \pythonilIdx{ArithmeticError} if its argument is non-finite or negative.
|
274 | 274 | This time, we want to compute~$\sqrt{\frac{1}{0}}$ and thus aim to store the result of \pythonil{sqrt(1 / 0)} in a variable \pythonil{sqrt_of_1_div_0}.
|
275 | 275 | We first declare the variable as a \pythonil{float}.
|
276 | 276 | Then, in a \pythonilIdx{try}-\pythonilIdx{except} block, we perform the actual computation: \pythonil{sqrt_of_1_div_0 = sqrt(1 / 0)}.
|
277 | 277 | Knowing that \pythonil{sqrt} might raise an \pythonilIdx{ArithmeticError}, we provide a corresponding \pythonilIdx{except} block.
|
278 |
| -However, we also know that \pythonil{1 / 0} looks a bit dodgy, as we also try to intercept a potential \pythonilIdx{DivisionByZero} error. |
| 278 | +However, we also know that \pythonil{1 / 0} looks a bit dodgy, as we also try to intercept a potential \pythonilIdx{ZeroDivisionError} error. |
279 | 279 | As you can see, we can have two independent \pythonilIdx{except} clauses.
|
280 | 280 |
|
281 | 281 | So, which one will be executed?
|
282 | 282 | Certainly, $\frac{1}{0}$ is not finite, so \pythonil{sqrt} will raise an \pythonilIdx{Exception}.
|
283 |
| -Then again, $\frac{1}{0}$ cannot be computed at all, so maybe we get a \pythonilIdx{DivisionByZero} error instead? |
284 |
| -We find that the \pythonilIdx{except} block for \pythonilIdx{DivisionByZero} is executed. |
| 283 | +Then again, $\frac{1}{0}$ cannot be computed at all, so maybe we get a \pythonilIdx{ZeroDivisionError} error instead? |
| 284 | +We find that the \pythonilIdx{except} block for \pythonilIdx{ZeroDivisionError} is executed. |
285 | 285 | The reason is that in order to invoke \pythonil{sqrt(1 / 0)}, the \python\ interpreter must first compute the result of~\pythonil{1 / 0}.
|
286 |
| -This computation raises \pythonilIdx{DivisionByZero} and \pythonil{sqrt} is never called. |
| 286 | +This computation raises \pythonilIdx{ZeroDivisionError} and \pythonil{sqrt} is never called. |
287 | 287 |
|
288 | 288 | This leads us to the question:
|
289 | 289 | If \pythonil{sqrt} is never called, then what will be assigned to \pythonil{sqrt_of_1_div_0}?
|
|
0 commit comments