|
168 | 168 |
|
169 | 169 | We collect the output of the program in \cref{exec:exceptions:use_sqrt_raise}. |
170 | 170 | As can be seen, for \pythonil{0.0}, \pythonil{1.0}, \pythonil{2.0}, \pythonil{4.0}, and \pythonil{10.0}, the results are printed as anticipated. |
171 | | -However, when the \pythonilIdx{for}~loop reaches \pythonilIdx{inf}, the program is terminated an the so-called \emph{stack trace} is written to the output. |
| 171 | +However, when the \pythonilIdx{for}~loop reaches \pythonilIdx{inf}, the program is terminated an the so-called \pgls{stackTrace} is written to the output. |
172 | 172 |
|
173 | 173 | It begins with the line \textil{Traceback (most recent call last):}. |
174 | 174 | In the first line following this text, the source code file and the index of the line in that file where the \pythonilIdx{Exception} was originally raised are printed. |
|
177 | 177 | Below that, we get to see the context of our \pythonil{sqrt} function: |
178 | 178 | First, the path to its module is given (ending in \textil{06_exceptions/sqrt_raise.py}) and it is pointed out that the \pythonilIdx{Exception} was raised in line~15. |
179 | 179 | This line of code is then also given, and it indeed is the one starting with \pythonil{raise ArithmeticError}. |
180 | | -The \emph{stack trace} therefore shows us exactly where the error happened and from where the code causing the error was called.% |
| 180 | +The \pgls{stackTrace} therefore shows us exactly where the error happened and from where the code causing the error was called.% |
181 | 181 | % |
182 | 182 | \begin{sloppypar}% |
183 | | -After the stack trace, we can see information about the error printed that we passed in: |
| 183 | +After the \pgls{stackTrace}, we can see information about the error printed that we passed in: |
184 | 184 | \textil{ArithmeticError: sqrt(inf) is not permitted.} |
185 | 185 | We made this message by ourselves using an \pgls{fstring} when raising the \pythonilIdx{Exception}. |
186 | 186 | We did this so that it tells the user that \pythonil{sqrt} was called with the argument \pythonil{inf} that we did not permit.% |
187 | 187 | \end{sloppypar}% |
188 | 188 | % |
189 | 189 | The above information allows us to pretty much identify the source of the problem. |
190 | | -It shall be stated here that new programmers often ignore the stack trace. |
| 190 | +It shall be stated here that new programmers often ignore the \pgls{stackTrace}. |
191 | 191 | They see that a program produces and error and then try to figure out why by looking at their code. |
192 | | -They often do not read the stack trace or the error information below it.% |
| 192 | +They often do not read the \pgls{stackTrace} or the error information below it.% |
193 | 193 | % |
194 | 194 | \bestPractice{exceptionStackTrace}{% |
195 | | -The stack trace and error information printed on the \python\ console in case of an uncaught \pythonilIdx{Exception} are essential information to identify the problem. % |
| 195 | +The \pgls{stackTrace} and error information printed on the \python\ console in case of an uncaught \pythonilIdx{Exception} are essential information to identify the problem. % |
196 | 196 | They should \emph{always} be read and understood before trying to improve the code.% |
197 | 197 | }% |
198 | 198 | % |
|
204 | 204 | Terminating the process may seem rash, but it is not. |
205 | 205 | If a programmer used our \pythonil{sqrt} function incorrectly, then this will force them to fix their error. |
206 | 206 | If the input \pythonil{inf} was the result of corrupted data, another erroneous computation, or an input mistake by the user, then terminating the program prevented this error from propagating. |
207 | | -For both scenarios, the stack trace and error output gives clear information about what went wrong and where.% |
| 207 | +For both scenarios, the \pgls{stackTrace} and error output gives clear information about what went wrong and where.% |
208 | 208 | % |
209 | 209 | \endhsection% |
210 | 210 | % |
|
296 | 296 | Trying to access it will \pythonilIdx{raise} a \pythonilIdx{NameError}. |
297 | 297 | While we did declare it, the \python\ interpreter does not know this variable yet, as it does not have a value. |
298 | 298 | Our program will terminate, because the \pythonilIdx{NameError} is never caught anywhere. |
299 | | -Instead, the stack trace and error information will be printed in \cref{exec:exceptions:try_multi_except}.% |
| 299 | +Instead, the \pgls{stackTrace} and error information will be printed in \cref{exec:exceptions:try_multi_except}.% |
300 | 300 | % |
301 | 301 | \bestPractice{noAssignAfterRaise}{% |
302 | 302 | If an \pythonilIdx{Exception} is raised, be aware that the control flow will immediately leave the current block. % |
|
0 commit comments