-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path6_exceptions.html
More file actions
845 lines (806 loc) · 91.9 KB
/
6_exceptions.html
File metadata and controls
845 lines (806 loc) · 91.9 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>6. Errors and exceptions — Object-oriented Programming documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=03e43079" />
<link rel="stylesheet" type="text/css" href="_static/fenics.css?v=8c7d05f9" />
<link rel="stylesheet" type="text/css" href="_static/proof.css" />
<link rel="stylesheet" type="text/css" href="_static/graphviz.css?v=fd3f3429" />
<script src="_static/documentation_options.js?v=5929fcd5"></script>
<script src="_static/doctools.js?v=9a2dae69"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="_static/proof.js"></script>
<script async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="7. Inheritance and composition" href="7_inheritance.html" />
<link rel="prev" title="5. Abstract data types" href="5_abstract_data_types.html" />
<!--[if lte IE 6]>
<link rel="stylesheet" href="_static/ie6.css" type="text/css" media="screen" charset="utf-8" />
<![endif]-->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-0EFVH5C4DC"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-0EFVH5C4DC');
</script>
<link rel="stylesheet" href="_static/featured.css">
<link rel="shortcut icon" href="_static/icon.ico" />
</head><body>
<div class="wrapper">
<a href="index.html"><img src="_static/banner.png" width="900px" alt="FInAT Project Banner" /></a>
<div id="access">
<div class="menu">
<ul>
<li class="page_item"><a href="index.html" title="Book">Book</a></li>
<li class="page_item"><a href="videos.html" title="Videos">Videos</a></li>
<li class="page_item"><a href="exercises.html"
title="Exercises">Exercises</a></li>
<li class="page_item"><a href="installation.html" title="Installation">Installation</a></li>
</ul>
</div><!-- .menu -->
</div><!-- #access -->
</div><!-- #wrapper -->
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="errors-and-exceptions">
<span id="id1"></span><h1><span class="section-number">6. </span>Errors and exceptions<a class="headerlink" href="#errors-and-exceptions" title="Link to this heading">¶</a></h1>
<details>
<summary>
Video: errors and exceptions.</summary><div class="video_wrapper" style="">
<iframe allowfullscreen="true" src="https://player.vimeo.com/video/509280820" style="border: 0; height: 345px; width: 560px">
</iframe></div><p>Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=9c437655-106e-47d2-85d4-ae1c00db1ca7">watch this video on Panopto</a>.</p>
</details><p>It is a sight familiar to every programmer: instead of producing the
desired result, the screen is filled with seemingly unintelligible
garbage because an error has occurred. Producing errors is an
unavoidable part of programming, so learning to understand and correct
them is an essential part of learning to program.</p>
<section id="what-is-an-error">
<h2><span class="section-number">6.1. </span>What is an error?<a class="headerlink" href="#what-is-an-error" title="Link to this heading">¶</a></h2>
<p>In mathematics, we are used to the idea that an expression might not
be defined. For example, in the absence of further information,
<span class="math notranslate nohighlight">\(0/0\)</span> does not have a well-defined value. Similarly, the string
of symbols <span class="math notranslate nohighlight">\(3 \times \%\)</span> does not have a mathematical
meaning. It is likewise very easy to create statements or expressions
in a programming language which either don’t have a well-defined
meaning, or which just don’t amount to a meaningful statement within
the rules of the language. A mathematician confronting an undefined
mathematical expression can do little else than throw up their hands
and ask the author what they meant. The <a class="reference internal" href="2_programs_in_files.html#term-Python-interpreter"><span class="xref std std-term">Python interpreter</span></a>, upon
encountering code which has no defined meaning, responds similarly;
though rather than raising its non-existent hands, it raises an
<a class="reference internal" href="#term-exception"><span class="xref std std-term">exception</span></a>. It is then up to the programmer to divine what to do next.</p>
<p>Let’s take a look at what Python does in response to a simple
error:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="mf">0.</span><span class="o">/</span><span class="mf">0.</span>
<span class="gt">--------------------------------------------------------------------------</span>
<span class="ne">ZeroDivisionError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="n">Cell</span> <span class="n">In</span> <span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">line</span> <span class="mi">1</span>
<span class="ne">----> </span><span class="mi">1</span> <span class="mf">0.</span><span class="o">/</span><span class="mf">0.</span>
<span class="ne">ZeroDivisionError</span>: float division by zero
</pre></div>
</div>
<p>An important rule in interpreting Python errors, the reasons for which we will
return to, is to always read the error message from the bottom up. In
this case, the last line contains the name of the exception which has
been raised, <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ZeroDivisionError" title="(in Python v3.14)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a>, followed by a colon, followed by
a descriptive string providing more information about what has gone
wrong. In this case, that more or less says the same as the exception
name, but that won’t be the case for all exceptions. The four lines
above the exception are called a <a class="reference internal" href="#term-traceback"><span class="xref std std-term">traceback</span></a>. We’ll return to
interpreting tracebacks presently. In this case the error is easy to interpret
and understand: the code divided the <a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> value <code class="xref py py-obj docutils literal notranslate"><span class="pre">0.</span></code> by another zero,
and this does not have a well-defined result in Python’s arithmetic system.</p>
<section id="syntax-errors">
<h3><span class="section-number">6.1.1. </span>Syntax errors<a class="headerlink" href="#syntax-errors" title="Link to this heading">¶</a></h3>
<p>Now consider the case of an expression that doesn’t make mathematical sense:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [5]: </span><span class="mi">3</span> <span class="o">*</span> <span class="o">%</span>
<span class="go">Cell In [5], line 1</span>
<span class="go"> 3 * %</span>
<span class="go"> ^</span>
<span class="go">SyntaxError: invalid syntax</span>
</pre></div>
</div>
<p>This creates a syntax error, signified by a <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#SyntaxError" title="(in Python v3.14)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> exception. In
programming languages, as with human languages, the syntax is the set of rules
which defines which expressions are well-formed. Notice that the earlier lines
of a syntax error appear somewhat different to those of the previous exception.
Almost all exceptions occur because the <a class="reference internal" href="2_programs_in_files.html#term-Python-interpreter"><span class="xref std std-term">Python interpreter</span></a> attempts to
evaluate a statement or expression and encounters a problem. Syntax errors are
a special case: when a syntax error occurs, the interpreter can’t even get as
far as attempting to evaluate because the sequence of characters it has been
asked to execute do not make sense in Python. This time, the error message
shows the precise point in the line at which the Python interpreter found a
problem. This is indicated by the caret symbol (<code class="xref py py-obj docutils literal notranslate"><span class="pre">^</span></code>). In this case, the reason
that the expression doesn’t make any sense is that the modulo operator (<code class="xref py py-obj docutils literal notranslate"><span class="pre">%</span></code>) is
not a permissible second operand to multiplication (<code class="xref py py-obj docutils literal notranslate"><span class="pre">*</span></code>), so the Python
interpreter places the caret under the modulo operator.</p>
<p>Even though the Python interpreter will highlight the point at which
the syntax doesn’t make sense, this might not quite actually be the
point at which you made the mistake. In particular, failing to finish
a line of code will often result in the interpreter assuming that the
expression continues on the next line of program text, resulting in
the syntax error appearing to be one line later than it really
occurs. Consider the following code:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>
</div>
<p>The error here is a missing closing bracket on the first line, however
the error message which the <a class="reference internal" href="2_programs_in_files.html#term-Python-interpreter"><span class="xref std std-term">Python interpreter</span></a> prints when this code is run is:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span> <span class="n">File</span> <span class="s2">"syntax_error.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">2</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="o">^</span>
<span class="ne">SyntaxError</span><span class="p">:</span> <span class="n">invalid</span> <span class="n">syntax</span>
</pre></div>
</div>
<p>To understand why Python reports the error on the line following the
actual problem, we need to understand that the missing closing bracket
was not by itself an error. The user could, after all, validly
continue the <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#tuple" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> constructor on the next line. For example,
the following code would be completely valid:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>
<span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>
</div>
<p>This means that the <a class="reference internal" href="2_programs_in_files.html#term-Python-interpreter"><span class="xref std std-term">Python interpreter</span></a> can only know that something is
wrong when it sees <a class="reference external" href="https://docs.python.org/3/library/functions.html#print" title="(in Python v3.14)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">print</span></code></a>, because <a class="reference external" href="https://docs.python.org/3/library/functions.html#print" title="(in Python v3.14)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">print</span></code></a> cannot follow <code class="xref py py-obj docutils literal notranslate"><span class="pre">2</span></code> in a
tuple constructor. The interpreter, therefore, reports that the <a class="reference external" href="https://docs.python.org/3/library/functions.html#print" title="(in Python v3.14)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">print</span></code></a>
is a syntax error.</p>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>If the Python interpreter reports a syntax error at the start of a
line, always check to see if the actual error is on the previous
line.</p>
</div>
</section>
</section>
<section id="exceptions">
<h2><span class="section-number">6.2. </span>Exceptions<a class="headerlink" href="#exceptions" title="Link to this heading">¶</a></h2>
<p>Aside from syntax errors, which are handled directly by the
interpreter, errors occur when Python code is executed and something
goes wrong. In these cases the Python code in which the problem is
encountered must signal this to the interpreter. It does this using a
special kind of object called an <a class="reference internal" href="#term-exception"><span class="xref std std-term">exception</span></a>. When an exception
occurs, the interpreter stops executing the usual sequence of Python
commands. Unless the programmer has taken special measures, to which
we will return in <a class="reference internal" href="#handling-exceptions"><span class="std std-numref">Section 6.5</span></a>, the execution will
cease and an error message will result.</p>
<p>Because there are many things that can go wrong, Python has many types
of exception built in. For example, if we attempt to access the number
2 position in a tuple with only two entries, then an
<a class="reference external" href="https://docs.python.org/3/library/exceptions.html#IndexError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">IndexError</span></code></a> exception occurs:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">2</span><span class="p">]</span>
<span class="gt">--------------------------------------------------------------------------</span>
<span class="ne">IndexError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="n">Cell</span> <span class="n">In</span> <span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">line</span> <span class="mi">1</span>
<span class="ne">----> </span><span class="mi">1</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">2</span><span class="p">]</span>
<span class="ne">IndexError</span>: tuple index out of range
</pre></div>
</div>
<p>The exception type provides some indication as
to what has gone wrong, and there is usually also an error message and
sometimes more data to help diagnose the problem. The <a class="reference external" href="https://docs.python.org/3/library/exceptions.html" title="(in Python v3.14)"><span class="xref std std-doc">full list
of built-in exceptions</span></a> is available in the
Python documentation. Python developers can define their own
exceptions so there are many more defined in third-party packages. We will
turn to the subject of defining new exception classes in
<a class="reference internal" href="7_inheritance.html#defining-exceptions"><span class="std std-numref">Section 7.4</span></a>.</p>
</section>
<section id="tracebacks-finding-errors">
<h2><span class="section-number">6.3. </span>Tracebacks: finding errors<a class="headerlink" href="#tracebacks-finding-errors" title="Link to this heading">¶</a></h2>
<details>
<summary>
Video: tracebacks.</summary><div class="video_wrapper" style="">
<iframe allowfullscreen="true" src="https://player.vimeo.com/video/509280880" style="border: 0; height: 345px; width: 560px">
</iframe></div><p>Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=55886da2-963f-43c3-9dc2-ae1c00db2513">watch this video on Panopto</a>.</p>
</details><p>The errors we have looked at so far have all been located in the top
level of code either typed directly into iPython or executed in a
script. However, what happens if an error occurs in a function call or
even several functions down? Consider the following code, which uses
the <code class="xref py py-class docutils literal notranslate"><span class="pre">Polynomial</span></code> class from
<a class="reference internal" href="3_objects.html#objects"><span class="std std-numref">Chapter 3</span></a>:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="kn">from</span><span class="w"> </span><span class="nn">example_code.polynomial</span><span class="w"> </span><span class="kn">import</span> <span class="n">Polynomial</span>
<span class="gp">In [2]: </span><span class="n">p</span> <span class="o">=</span> <span class="n">Polynomial</span><span class="p">((</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">))</span>
<span class="gp">In [3]: </span><span class="nb">print</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="go">bx + a</span>
</pre></div>
</div>
<p>Perhaps surprisingly, it turns out that we are able to define a polynomial
whose coefficients are letters, and we can even print the resulting object.
However, if we attempt to add this polynomial to the number 1, we are in
trouble:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">p</span><span class="p">)</span>
<span class="gt">--------------------------------------------------------------------------</span>
<span class="ne">TypeError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="n">Cell</span> <span class="n">In</span> <span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="n">line</span> <span class="mi">1</span>
<span class="ne">----> </span><span class="mi">1</span> <span class="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">p</span><span class="p">)</span>
<span class="nn">File ~/docs/principles_of_programming/object-oriented-programming/example_code/polynomial.py:59,</span> in <span class="ni">Polynomial.__radd__</span><span class="nt">(self, other)</span>
<span class="g g-Whitespace"> </span><span class="mi">58</span> <span class="k">def</span><span class="w"> </span><span class="fm">__radd__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="ne">---> </span><span class="mi">59</span> <span class="k">return</span> <span class="bp">self</span> <span class="o">+</span> <span class="n">other</span>
<span class="nn">File ~/docs/principles_of_programming/object-oriented-programming/example_code/polynomial.py:38,</span> in <span class="ni">Polynomial.__add__</span><span class="nt">(self, other)</span>
<span class="g g-Whitespace"> </span><span class="mi">36</span> <span class="k">def</span><span class="w"> </span><span class="fm">__add__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="g g-Whitespace"> </span><span class="mi">37</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Number</span><span class="p">):</span>
<span class="ne">---> </span><span class="mi">38</span> <span class="k">return</span> <span class="n">Polynomial</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">coefficients</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">other</span><span class="p">,)</span>
<span class="g g-Whitespace"> </span><span class="mi">39</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">coefficients</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
<span class="g g-Whitespace"> </span><span class="mi">41</span> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Polynomial</span><span class="p">):</span>
<span class="g g-Whitespace"> </span><span class="mi">42</span> <span class="c1"># Work out how many coefficient places the two polynomials have in</span>
<span class="g g-Whitespace"> </span><span class="mi">43</span> <span class="c1"># common.</span>
<span class="g g-Whitespace"> </span><span class="mi">44</span> <span class="n">common</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">degree</span><span class="p">(),</span> <span class="n">other</span><span class="o">.</span><span class="n">degree</span><span class="p">())</span> <span class="o">+</span> <span class="mi">1</span>
<span class="ne">TypeError</span>: can only concatenate str (not "int") to str
</pre></div>
</div>
<p>This is a much larger error message than those we have previously
encountered, however, the same principles apply. We start by reading
the last line. This tells us that the error was a <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code></a>
caused by attempting to <a class="reference internal" href="3_objects.html#term-concatenation"><span class="xref std std-term">concatenate</span></a> (add) an integer to a
string. Where did this error occur? This is a more involved question
than it may first appear, and the rest of the error message above is
designed to help us answer this question. This type of error message
is called a <a class="reference internal" href="#term-traceback"><span class="xref std std-term">traceback</span></a>, as the second line of the error message
suggests. In order to understand this message, we need to understand a
little about how a Python program is executed, and in particular about
the call stack.</p>
<section id="the-call-stack">
<span id="call-stack"></span><h3><span class="section-number">6.3.1. </span>The call stack<a class="headerlink" href="#the-call-stack" title="Link to this heading">¶</a></h3>
<details>
<summary>
Video: the call stack.</summary><div class="video_wrapper" style="">
<iframe allowfullscreen="true" src="https://player.vimeo.com/video/509281576" style="border: 0; height: 345px; width: 560px">
</iframe></div><p>Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=f4fc8938-8e22-47bb-ac78-ae1c00db2e41">watch this video on Panopto</a>.</p>
</details><p>A Python program is a sequence of Python statements, which are
executed in a sequence determined by the flow control logic of the
program itself. Each statement contains zero or more function calls <a class="footnote-reference brackets" href="#function" id="id2" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a>,
which are executed in the course of evaluating that statement.</p>
<p>One of the most basic features of a function call is that the contents
of the function execute, and then the code which called the function
continues on from the point of the function call, using the return
value of the function in place of the call. Let’s think about what
happens when this occurs. Before calling the function, there is a
large amount of information which describes the context of the current
program execution. For example, there are all of the module, function,
and variable names which are in scope, and there is the record of
which instruction is next to be executed. This collection of
information about the current execution context is called a
<a class="reference internal" href="#term-stack-frame"><span class="xref std std-term">stack frame</span></a>. We learned about <a class="reference internal" href="5_abstract_data_types.html#term-stack"><span class="xref std std-term">stacks</span></a> in
<a class="reference internal" href="5_abstract_data_types.html#stacks"><span class="std std-numref">Section 5.1</span></a>, and the term “stack frame” is not a coincidence. The
Python interpreter maintains a <a class="reference internal" href="5_abstract_data_types.html#term-stack"><span class="xref std std-term">stack</span></a> of stack frames called
the <a class="reference internal" href="#term-call-stack"><span class="xref std std-term">call stack</span></a>. It is also sometimes called the
<a class="reference internal" href="#term-execution-stack"><span class="xref std std-term">execution stack</span></a> or <a class="reference internal" href="#term-interpreter-stack"><span class="xref std std-term">interpreter stack</span></a>.</p>
<p>The first frame on the stack contains the execution context for the
Python script that the user ran or, in the case where the user worked
interactively, for the iPython shell or Jupyter notebook into which
the user was typing. When a function is called, the Python interpreter
creates a new stack frame containing the local execution context of
that function and pushes it onto the call stack. When that function
returns, its stack frame is popped from the call stack, leaving the
interpreter to continue at the next instruction in the stack frame
from which the function was called. Because functions can call
functions which call functions and so on in a nearly limitless
sequence, there can be a number of stack frames in existence at any
time.</p>
</section>
<section id="interpreting-tracebacks">
<h3><span class="section-number">6.3.2. </span>Interpreting tracebacks<a class="headerlink" href="#interpreting-tracebacks" title="Link to this heading">¶</a></h3>
<p>Let’s return to the traceback for our erroneous polynomial addition:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">p</span><span class="p">)</span>
<span class="gt">--------------------------------------------------------------------------</span>
<span class="ne">TypeError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="n">Cell</span> <span class="n">In</span> <span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="n">line</span> <span class="mi">1</span>
<span class="ne">----> </span><span class="mi">1</span> <span class="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">p</span><span class="p">)</span>
<span class="nn">File ~/docs/principles_of_programming/object-oriented-programming/example_code/polynomial.py:59,</span> in <span class="ni">Polynomial.__radd__</span><span class="nt">(self, other)</span>
<span class="g g-Whitespace"> </span><span class="mi">58</span> <span class="k">def</span><span class="w"> </span><span class="fm">__radd__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="ne">---> </span><span class="mi">59</span> <span class="k">return</span> <span class="bp">self</span> <span class="o">+</span> <span class="n">other</span>
<span class="nn">File ~/docs/principles_of_programming/object-oriented-programming/example_code/polynomial.py:38,</span> in <span class="ni">Polynomial.__add__</span><span class="nt">(self, other)</span>
<span class="g g-Whitespace"> </span><span class="mi">36</span> <span class="k">def</span><span class="w"> </span><span class="fm">__add__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="g g-Whitespace"> </span><span class="mi">37</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Number</span><span class="p">):</span>
<span class="ne">---> </span><span class="mi">38</span> <span class="k">return</span> <span class="n">Polynomial</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">coefficients</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">other</span><span class="p">,)</span>
<span class="g g-Whitespace"> </span><span class="mi">39</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">coefficients</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
<span class="g g-Whitespace"> </span><span class="mi">41</span> <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Polynomial</span><span class="p">):</span>
<span class="g g-Whitespace"> </span><span class="mi">42</span> <span class="c1"># Work out how many coefficient places the two polynomials have in</span>
<span class="g g-Whitespace"> </span><span class="mi">43</span> <span class="c1"># common.</span>
<span class="g g-Whitespace"> </span><span class="mi">44</span> <span class="n">common</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">degree</span><span class="p">(),</span> <span class="n">other</span><span class="o">.</span><span class="n">degree</span><span class="p">())</span> <span class="o">+</span> <span class="mi">1</span>
<span class="ne">TypeError</span>: can only concatenate str (not "int") to str
</pre></div>
</div>
<p>This shows information about a <a class="reference internal" href="#term-call-stack"><span class="xref std std-term">call stack</span></a> comprising three
<a class="reference internal" href="#term-stack-frame"><span class="xref std std-term">stack frames</span></a>. Look first at the bottom-most
frame, which corresponds to the function in which the exception
occurred. The traceback for this frame starts:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">File</span> <span class="o">~/</span><span class="n">docs</span><span class="o">/</span><span class="n">principles_of_programming</span><span class="o">/</span><span class="nb">object</span><span class="o">-</span><span class="n">oriented</span><span class="o">-</span><span class="n">programming</span><span class="o">/</span><span class="n">example_code</span><span class="o">/</span><span class="n">polynomial</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">38</span><span class="p">,</span> <span class="ow">in</span> <span class="n">Polynomial</span><span class="o">.</span><span class="fm">__add__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">)</span>
</pre></div>
</div>
<p>This indicates that the frame describes code in the file <code class="xref py py-obj docutils literal notranslate"><span class="pre">polynomial.py</span></code>
(which, on the author’s computer, is located in the folder
<code class="xref py py-obj docutils literal notranslate"><span class="pre">~/principles_of_programming/object-oriented-programming/example_code/</span></code>).
Specifically, the stack frame describes the execution of the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__add__()</span></code>
method, which is the <a class="reference internal" href="3_objects.html#term-special-method"><span class="xref std std-term">special method</span></a> responsible for polynomial
addition. The lines below this show the line on which execution stopped (line
38, in this case) and a couple of lines on either side, for context.</p>
<p>The stack frame above this shows the function from which the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__add__()</span></code>
method was called. In this case, this is the reverse addition <a class="reference internal" href="3_objects.html#term-special-method"><span class="xref std std-term">special
method</span></a>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__radd__()</span></code>. On line 59 <code class="xref py py-meth docutils literal notranslate"><span class="pre">__radd__()</span></code> calls <code class="xref py py-meth docutils literal notranslate"><span class="pre">__add__()</span></code>
through the addition of <code class="xref py py-obj docutils literal notranslate"><span class="pre">self</span></code> and <code class="xref py py-obj docutils literal notranslate"><span class="pre">other</span></code>.</p>
<p>Finally, the top stack frame corresponds to the command that the user typed in
iPython. This stack frame looks a little different from the others. Instead of
a file name there and a function name there is <code class="xref py py-obj docutils literal notranslate"><span class="pre">Cell</span> <span class="pre">In</span> <span class="pre">[4],</span> <span class="pre">line</span> <span class="pre">1</span></code>. This
indicates that the exception was raised on line 1 of the IPython cell <code class="xref py py-obj docutils literal notranslate"><span class="pre">In</span> <span class="pre">[4]</span></code>.</p>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>Older versions of Python display less helpful location information for the
top stack frame, so in that case you might see something like
<code class="xref py py-obj docutils literal notranslate"><span class="pre"><ipython-input-2-c3aeb16193d4></span> <span class="pre">in</span></code> rather than
<code class="xref py py-obj docutils literal notranslate"><span class="pre">Cell</span> <span class="pre">In</span> <span class="pre">[4],</span> <span class="pre">line</span> <span class="pre">1</span></code>.</p>
</div>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>The proximate cause of the error will be in the last <a class="reference internal" href="#term-stack-frame"><span class="xref std std-term">stack
frame</span></a> printed, so always read the <a class="reference internal" href="#term-traceback"><span class="xref std std-term">traceback</span></a> from the
bottom up. However, the ultimate cause of the problem may
be further up the <a class="reference internal" href="#term-call-stack"><span class="xref std std-term">call stack</span></a>, so don’t stop reading at the
bottom frame!</p>
</div>
</section>
</section>
<section id="raising-exceptions">
<span id="id3"></span><h2><span class="section-number">6.4. </span>Raising exceptions<a class="headerlink" href="#raising-exceptions" title="Link to this heading">¶</a></h2>
<details>
<summary>
Video: raising an exception.</summary><div class="video_wrapper" style="">
<iframe allowfullscreen="true" src="https://player.vimeo.com/video/509492490" style="border: 0; height: 345px; width: 560px">
</iframe></div><p>Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=fe0a11e0-4600-4578-9207-ae1c00db3aa1">watch this video on Panopto</a>.</p>
</details><p>Thus far we’ve noticed that an exception occurs when something goes
wrong in a program, and that the <a class="reference internal" href="2_programs_in_files.html#term-Python-interpreter"><span class="xref std std-term">Python interpreter</span></a> will stop
at that point and print out a <a class="reference internal" href="#term-traceback"><span class="xref std std-term">traceback</span></a>. We’ll now examine the
process by which an exception occurs.</p>
<p>An exception is triggered using the <a class="reference external" href="https://docs.python.org/3/reference/simple_stmts.html#raise" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a> keyword. For
example, suppose we want to ensure that the input to our Fibonacci
function is an integer. All Python integers are <a class="reference internal" href="3_objects.html#term-instance"><span class="xref std std-term">instances</span></a> of <a class="reference external" href="https://docs.python.org/3/library/numbers.html#numbers.Integral" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">numbers.Integral</span></code></a>, so we can check this. If we
find a non-integer type then the consequence should be a
<a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code></a>. This is achieved by <em>raising</em> the appropriate
exception, using the <a class="reference external" href="https://docs.python.org/3/reference/simple_stmts.html#raise" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a> statement. The keyword
<a class="reference external" href="https://docs.python.org/3/reference/simple_stmts.html#raise" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a> is followed by the exception. Almost all exceptions
take a string argument, which is the error message to be printed. In
<a class="reference internal" href="#typesafe-fib"><span class="std std-numref">Listing 6.1</span></a>, we inform the user that we were expecting an
integer rather than the type actually provided.</p>
<div class="literal-block-wrapper docutils container" id="id7">
<span id="typesafe-fib"></span><div class="code-block-caption"><span class="caption-number">Listing 6.1 </span><span class="caption-text">A version of the Fibonacci function which raises an
exception if a non-integer type is passed as the
argument.</span><a class="headerlink" href="#id7" title="Link to this code">¶</a></div>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kn">from</span><span class="w"> </span><span class="nn">numbers</span><span class="w"> </span><span class="kn">import</span> <span class="n">Integral</span>
<span class="linenos"> 2</span>
<span class="linenos"> 3</span>
<span class="linenos"> 4</span><span class="k">def</span><span class="w"> </span><span class="nf">typesafe_fib</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="sd">"""Return the n-th Fibonacci number, raising an exception if a</span>
<span class="linenos"> 6</span><span class="sd"> non-integer is passed as n."""</span>
<span class="linenos"> 7</span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">Integral</span><span class="p">):</span>
<span class="hll"><span class="linenos"> 8</span> <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
</span><span class="hll"><span class="linenos"> 9</span> <span class="sa">f</span><span class="s2">"fib expects an integer, not a </span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2">"</span>
</span><span class="hll"><span class="linenos">10</span> <span class="p">)</span>
</span><span class="linenos">11</span> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="linenos">12</span> <span class="k">return</span> <span class="mi">0</span>
<span class="linenos">13</span> <span class="k">elif</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="linenos">14</span> <span class="k">return</span> <span class="mi">1</span>
<span class="linenos">15</span> <span class="k">else</span><span class="p">:</span>
<span class="linenos">16</span> <span class="k">return</span> <span class="n">fib</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">fib</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>If we now pass a non-integer value to this function, we observe the following:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="kn">from</span><span class="w"> </span><span class="nn">fibonacci.typesafe_fibonacci</span><span class="w"> </span><span class="kn">import</span> <span class="n">typesafe_fib</span>
<span class="gp">In [2]: </span><span class="n">typesafe_fib</span><span class="p">(</span><span class="mf">1.5</span><span class="p">)</span>
<span class="gt">--------------------------------------------------------------------------</span>
<span class="ne">TypeError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="n">Cell</span> <span class="n">In</span> <span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">line</span> <span class="mi">1</span>
<span class="ne">----> </span><span class="mi">1</span> <span class="n">typesafe_fib</span><span class="p">(</span><span class="mf">1.5</span><span class="p">)</span>
<span class="nn">File ~/docs/principles_of_programming/object-oriented-programming/fibonacci/typesafe_fibonacci.py:8,</span> in <span class="ni">typesafe_fib</span><span class="nt">(n)</span>
<span class="g g-Whitespace"> </span><span class="mi">5</span><span class="w"> </span><span class="sd">"""Return the n-th Fibonacci number, raising an exception if a</span>
<span class="g g-Whitespace"> </span><span class="mi">6</span><span class="sd"> non-integer is passed as n."""</span>
<span class="g g-Whitespace"> </span><span class="mi">7</span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">Integral</span><span class="p">):</span>
<span class="ne">----> </span><span class="mi">8</span> <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
<span class="g g-Whitespace"> </span><span class="mi">9</span> <span class="sa">f</span><span class="s2">"fib expects an integer, not a </span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2">"</span>
<span class="g g-Whitespace"> </span><span class="mi">10</span> <span class="p">)</span>
<span class="g g-Whitespace"> </span><span class="mi">11</span> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="g g-Whitespace"> </span><span class="mi">12</span> <span class="k">return</span> <span class="mi">0</span>
<span class="ne">TypeError</span>: fib expects an integer, not a float
</pre></div>
</div>
<p>This is exactly what we have come to expect: execution has stopped and
we see a <a class="reference internal" href="#term-traceback"><span class="xref std std-term">traceback</span></a>. Notice that the final line is the error
message that we passed to <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code></a>. The only difference
between this and the previous errors we have seen is that the bottom
<a class="reference internal" href="#term-stack-frame"><span class="xref std std-term">stack frame</span></a> explicitly shows the exception being raised, while
previously the stack showed a piece of code where an error had
occurred. This minor difference has to do with whether the particular
piece of code where the exception occurred is written in Python, or is
written in a language such as C and called from Python. This
distinction is of negligible importance for our current purposes.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>An exceptionally common mistake that programmers make when first
trying to work with exceptions is to write:</p>
<div class="badcode docutils container">
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="ne">Exception</span>
</pre></div>
</div>
</div>
<p>instead of:</p>
<div class="goodcode docutils container">
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">raise</span> <span class="ne">Exception</span>
</pre></div>
</div>
</div>
<p>This mistake is the result of a confusion about what
<a class="reference external" href="https://docs.python.org/3/reference/simple_stmts.html#return" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> and <a class="reference external" href="https://docs.python.org/3/reference/simple_stmts.html#raise" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a> do. <a class="reference external" href="https://docs.python.org/3/reference/simple_stmts.html#return" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> means
“the function is finished, here is the result”. <a class="reference external" href="https://docs.python.org/3/reference/simple_stmts.html#raise" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a>
means “something exceptional happened, execution is stopping
without a result”.</p>
</div>
</section>
<section id="handling-exceptions">
<span id="id4"></span><h2><span class="section-number">6.5. </span>Handling exceptions<a class="headerlink" href="#handling-exceptions" title="Link to this heading">¶</a></h2>
<details>
<summary>
Video: handling exceptions.</summary><div class="video_wrapper" style="">
<iframe allowfullscreen="true" src="https://player.vimeo.com/video/509492495" style="border: 0; height: 345px; width: 560px">
</iframe></div><p>Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=1dfd1930-5b27-4858-8fd1-ae1c00db4473">watch this video on Panopto</a>.</p>
</details><p>So far we have seen several different sorts of exception, how to raise them,
and how to understand the resulting <a class="reference internal" href="#term-traceback"><span class="xref std std-term">traceback</span></a>. The <a class="reference internal" href="#term-traceback"><span class="xref std std-term">traceback</span></a> is
very helpful if the exception was caused by a bug in our code, as it is a rich
source of the information needed to understand and correct the error. However,
sometimes an exception is a valid result of a valid input, and we just need the
program to do something out of the ordinary to deal with the situation. For
example, Euclid’s algorithm for finding the greatest common divisor of
<span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> can very nearly be written recursively as:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">gcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="k">return</span> <span class="n">gcd</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">%</span> <span class="n">b</span><span class="p">)</span>
</pre></div>
</div>
<p>This works right up to the point where <code class="xref py py-obj docutils literal notranslate"><span class="pre">b</span></code> becomes zero, at which
point we should stop the recursion and return <code class="xref py py-obj docutils literal notranslate"><span class="pre">a</span></code>. What actually
happens if we run this code? Let’s try:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [2]: </span><span class="n">gcd</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">)</span>
<span class="gt">--------------------------------------------------------------------------</span>
<span class="ne">ZeroDivisionError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="n">Cell</span> <span class="n">In</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">line</span> <span class="mi">1</span>
<span class="ne">----> </span><span class="mi">1</span> <span class="n">gcd</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">)</span>
<span class="nn">Cell In[1], line 2,</span> in <span class="ni">gcd</span><span class="nt">(a, b)</span>
<span class="g g-Whitespace"> </span><span class="mi">1</span> <span class="k">def</span><span class="w"> </span><span class="nf">gcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="ne">----> </span><span class="mi">2</span> <span class="k">return</span> <span class="n">gcd</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">%</span> <span class="n">b</span><span class="p">)</span>
<span class="nn">Cell In[1], line 2,</span> in <span class="ni">gcd</span><span class="nt">(a, b)</span>
<span class="g g-Whitespace"> </span><span class="mi">1</span> <span class="k">def</span><span class="w"> </span><span class="nf">gcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="ne">----> </span><span class="mi">2</span> <span class="k">return</span> <span class="n">gcd</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">%</span> <span class="n">b</span><span class="p">)</span>
<span class="p">[</span><span class="o">...</span> <span class="n">skipping</span> <span class="n">similar</span> <span class="n">frames</span><span class="p">:</span> <span class="n">gcd</span> <span class="n">at</span> <span class="n">line</span> <span class="mi">2</span> <span class="p">(</span><span class="mi">1</span> <span class="n">times</span><span class="p">)]</span>
<span class="nn">Cell In[1], line 2,</span> in <span class="ni">gcd</span><span class="nt">(a, b)</span>
<span class="g g-Whitespace"> </span><span class="mi">1</span> <span class="k">def</span><span class="w"> </span><span class="nf">gcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="ne">----> </span><span class="mi">2</span> <span class="k">return</span> <span class="n">gcd</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">%</span> <span class="n">b</span><span class="p">)</span>
<span class="ne">ZeroDivisionError</span>: integer modulo by zero
</pre></div>
</div>
<p>Notice how the recursive call to <code class="xref py py-func docutils literal notranslate"><span class="pre">gcd()</span></code> causes several
<a class="reference internal" href="#term-stack-frame"><span class="xref std std-term">stack frames</span></a> that look the same. Indeed, the Python
interpreter even notices the similarity and skips over one. That makes
sense: <code class="xref py py-func docutils literal notranslate"><span class="pre">gcd()</span></code> calls itself until <code class="xref py py-obj docutils literal notranslate"><span class="pre">b</span></code> is zero, and then we get a
<a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ZeroDivisionError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a> because modulo zero is undefined. To
complete this function, what we need to do is to tell Python to stop
at the <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ZeroDivisionError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a> and return <code class="xref py py-obj docutils literal notranslate"><span class="pre">a</span></code>
instead. <a class="reference internal" href="#gcd"><span class="std std-numref">Listing 6.2</span></a> illustrates how this can be achieved.</p>
<div class="literal-block-wrapper docutils container" id="id8">
<span id="gcd"></span><div class="code-block-caption"><span class="caption-number">Listing 6.2 </span><span class="caption-text">A recursive implementation of Euclid’s algorithm which
catches the <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ZeroDivisionError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a> to implement the
base case.</span><a class="headerlink" href="#id8" title="Link to this code">¶</a></div>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="linenos">1</span><span class="k">def</span><span class="w"> </span><span class="nf">gcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="hll"><span class="linenos">2</span> <span class="k">try</span><span class="p">:</span>
</span><span class="linenos">3</span> <span class="k">return</span> <span class="n">gcd</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">%</span> <span class="n">b</span><span class="p">)</span>
<span class="hll"><span class="linenos">4</span> <span class="k">except</span> <span class="ne">ZeroDivisionError</span><span class="p">:</span>
</span><span class="hll"><span class="linenos">5</span> <span class="k">return</span> <span class="n">a</span>
</span></pre></div>
</div>
</div>
<p>The new structure here is the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a>… <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a>
block. The <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> keyword defines a block of code, in this
case just containing <code class="xref py py-obj docutils literal notranslate"><span class="pre">return</span> <span class="pre">gcd(b,</span> <span class="pre">a</span> <span class="pre">%</span> <span class="pre">b)</span></code>. The <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> is
optionally followed by an exception class, or a tuple of exception
classes. This case, the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> is only followed by the
<a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ZeroDivisionError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a> class. What this means is that if a
<a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ZeroDivisionError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a> is raised by any of the code inside the
<a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> block then, instead of execution halting and a
<a class="reference internal" href="#term-traceback"><span class="xref std std-term">traceback</span></a> being printed, the code inside the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a>
block is run.</p>
<p>In the example here, this means that once <code class="xref py py-obj docutils literal notranslate"><span class="pre">b</span></code> is zero, instead of
<code class="xref py py-obj docutils literal notranslate"><span class="pre">gcd</span></code> being called a further time, a is returned. If we run this
version of <code class="xref py py-func docutils literal notranslate"><span class="pre">gcd()</span></code> then we have, as we might expect:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [2]: </span><span class="n">gcd</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">)</span>
<span class="gh">Out[2]: </span><span class="go">2</span>
</pre></div>
</div>
<section id="except-clauses">
<h3><span class="section-number">6.5.1. </span>Except clauses<a class="headerlink" href="#except-clauses" title="Link to this heading">¶</a></h3>
<details>
<summary>
Video: further exception handling.</summary><div class="video_wrapper" style="">
<iframe allowfullscreen="true" src="https://player.vimeo.com/video/509492496" style="border: 0; height: 345px; width: 560px">
</iframe></div><p>Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=4819cfe6-2743-4ed2-986e-af8e00c2198a">watch this video on Panopto</a>.</p>
</details><p>Let’s look in a little more detail at how <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> works. The full
version of the except statement takes a tuple of exception classes. If an
exception is raised matching any of the exceptions in that tuple then the code
in the except block is executed.</p>
<p>It’s also possible to have more than one <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> block following a
single <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> statement. In this case, the first except block for which
the exception matches the list of exceptions is executed. For example:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="k">try</span><span class="p">:</span>
<span class="go"> ...: 0./0</span>
<span class="go"> ...: except TypeError, KeyError:</span>
<span class="go"> ...: print("Type or key error")</span>
<span class="go"> ...: except ZeroDivisionError:</span>
<span class="go"> ...: print("Zero division error")</span>
<span class="go"> ...:</span>
<span class="go">Zero division error</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>It is also possible to omit the list of exceptions after <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a>.
In this case, the except block will match any exception which is raised in
the corresponding try block. Using unconstrained except blocks like this is
a somewhat dangerous strategy. Usually, the except block will be designed
to deal with a particular type of exceptional circumstance. However, an
except block that catches any exception may well be triggered by a completely
different exception, in which case it will just make the error more
confusing by obscuring where the issue actually occurred.</p>
</div>
</section>
<section id="else-and-finally">
<span id="else-finally"></span><h3><span class="section-number">6.5.2. </span>Else and finally<a class="headerlink" href="#else-and-finally" title="Link to this heading">¶</a></h3>
<p>It can also be useful to execute some code only if an exception is not raised.
This can be achieved using an <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">else</span></code></a> clause. An <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">else</span></code></a> clause after a <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> block is caused only if no exception was
raised.</p>
<p>It is also sometimes useful to be able to execute some code no matter what
happened in the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> block. If there is a <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#finally" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> clause
then the code it contains will be executed whether or not an exception is
raised and whether or not any exception is handled by an <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a>
clause. The contents of the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#finally" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> clause will always execute. This
may be useful, for example, if it is necessary to close an external file or
network connection at the end of an operation, even if an exception is raised.
The full details of the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#finally" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> clause are covered in the
<a class="reference external" href="https://docs.python.org/3/tutorial/errors.html#tut-handling" title="(in Python v3.14)"><span class="xref std std-ref">section of the official Python tutorial on handling exceptions</span></a>.</p>
<p>This plethora of variants on the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> block can get a little
confusing, so a practical example may help. <a class="reference internal" href="#except-demo"><span class="std std-numref">Listing 6.3</span></a> prints out a
different message for each type of clause.</p>
<div class="literal-block-wrapper docutils container" id="id9">
<span id="except-demo"></span><div class="code-block-caption"><span class="caption-number">Listing 6.3 </span><span class="caption-text">A demonstration of all the clauses of the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> block.</span><a class="headerlink" href="#id9" title="Link to this code">¶</a></div>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="k">def</span><span class="w"> </span><span class="nf">except_demo</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="linenos"> 2</span><span class="w"> </span><span class="sd">"""Demonstrate all the clauses of a `try` block."""</span>
<span class="linenos"> 3</span>
<span class="linenos"> 4</span> <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Attempting division by </span><span class="si">{</span><span class="n">n</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="linenos"> 5</span> <span class="k">try</span><span class="p">:</span>
<span class="linenos"> 6</span> <span class="nb">print</span><span class="p">(</span><span class="mf">0.</span><span class="o">/</span><span class="n">n</span><span class="p">)</span>
<span class="linenos"> 7</span> <span class="k">except</span> <span class="ne">ZeroDivisionError</span><span class="p">:</span>
<span class="linenos"> 8</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Zero division"</span><span class="p">)</span>
<span class="linenos"> 9</span> <span class="k">else</span><span class="p">:</span>
<span class="linenos">10</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Division successful."</span><span class="p">)</span>
<span class="linenos">11</span> <span class="k">finally</span><span class="p">:</span>
<span class="linenos">12</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Finishing up."</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>If we execute <a class="reference internal" href="example_code.html#example_code.try_except.except_demo" title="example_code.try_except.except_demo"><code class="xref py py-func docutils literal notranslate"><span class="pre">except_demo()</span></code></a> for a variety of
arguments, we can observe this complete <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> block in action. First,
we provide an input which is a valid divisor:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="kn">from</span><span class="w"> </span><span class="nn">example_code.try_except</span><span class="w"> </span><span class="kn">import</span> <span class="n">except_demo</span>
<span class="gp">In [2]: </span><span class="n">except_demo</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">Attempting division by 1</span>
<span class="go">0.0</span>
<span class="go">Division successful.</span>
<span class="go">Finishing up.</span>
</pre></div>
</div>
<p>Here we can see the output of the division, the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">else</span></code></a> block, and
the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#finally" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> block. Next we divide by zero:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="n">except_demo</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">Attempting division by 0</span>
<span class="go">Zero division</span>
<span class="go">Finishing up.</span>
</pre></div>
</div>
<p>This caused a <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ZeroDivisionError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a>, which was caught by the first
<a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> clause. Since an exception was raised, the the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">else</span></code></a> block is not executed, but the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#finally" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> block still executes.
Finally, if we attempt to divide by a string, the exception is not handled, but
the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#finally" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> block executes before the exception causes a traceback:</p>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="n">except_demo</span><span class="p">(</span><span class="s2">"frog"</span><span class="p">)</span>
<span class="go">Attempting division by frog</span>
<span class="go">Finishing up.</span>
<span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">TypeError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="n">Cell</span> <span class="n">In</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="n">line</span> <span class="mi">1</span>
<span class="ne">----> </span><span class="mi">1</span> <span class="n">except_demo</span><span class="p">(</span><span class="s2">"frog"</span><span class="p">)</span>
<span class="nn">Cell In[3], line 6,</span> in <span class="ni">except_demo</span><span class="nt">(n)</span>
<span class="g g-Whitespace"> </span><span class="mi">4</span> <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Attempting division by </span><span class="si">{</span><span class="n">n</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="g g-Whitespace"> </span><span class="mi">5</span> <span class="k">try</span><span class="p">:</span>
<span class="ne">----> </span><span class="mi">6</span> <span class="nb">print</span><span class="p">(</span><span class="mf">0.</span><span class="o">/</span><span class="n">n</span><span class="p">)</span>
<span class="g g-Whitespace"> </span><span class="mi">7</span> <span class="k">except</span> <span class="ne">ZeroDivisionError</span><span class="p">:</span>
<span class="g g-Whitespace"> </span><span class="mi">8</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Zero division"</span><span class="p">)</span>
<span class="ne">TypeError</span>: unsupported operand type(s) for /: 'float' and 'str'
</pre></div>
</div>
</section>
<section id="exception-handling-and-the-call-stack">
<h3><span class="section-number">6.5.3. </span>Exception handling and the call stack<a class="headerlink" href="#exception-handling-and-the-call-stack" title="Link to this heading">¶</a></h3>
<p>An <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> block will handle any matching exception raised in the
preceding <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> block. The <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> block can, of
course, contain any code at all. In particular it might contain
function calls which themselves may well call further functions. This
means that an exception might occur several <a class="reference internal" href="#term-stack-frame"><span class="xref std std-term">stack frames</span></a> down the <a class="reference internal" href="#term-call-stack"><span class="xref std std-term">call stack</span></a> from the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a>
clause. Indeed, some of the functions called might themselves contain
<a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> blocks with the result that an exception is raised at a
point which is ultimately inside several <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> blocks.</p>
<p>The <a class="reference internal" href="2_programs_in_files.html#term-Python-interpreter"><span class="xref std std-term">Python interpreter</span></a> deals with this situation by starting from the
current <a class="reference internal" href="#term-stack-frame"><span class="xref std std-term">stack frame</span></a> and working upwards, a process known as <em>unwinding
the stack</em>. <a class="reference internal" href="#unwind"><span class="std std-numref">Listing 6.4</span></a> shows pseudocode for this process.</p>
<div class="literal-block-wrapper docutils container" id="id10">
<span id="unwind"></span><div class="code-block-caption"><span class="caption-number">Listing 6.4 </span><span class="caption-text">Pseudocode for the process of <em>unwinding the stack</em>, in which the
interpreter successively looks through higher stack frames to search
for an <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.14)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> clause matching the exception that has just
been raised.</span><a class="headerlink" href="#id10" title="Link to this code">¶</a></div>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="n">call</span> <span class="n">stack</span> <span class="ow">not</span> <span class="n">empty</span><span class="p">:</span>
<span class="k">if</span> <span class="n">current</span> <span class="n">execution</span> <span class="n">point</span> <span class="ow">is</span> <span class="ow">in</span> <span class="n">a</span> <span class="k">try</span> <span class="n">block</span> \
<span class="k">with</span> <span class="n">an</span> <span class="k">except</span> <span class="n">matching</span> <span class="n">the</span> <span class="n">current</span> <span class="n">exception</span><span class="p">:</span>
<span class="n">execution</span> <span class="n">continues</span> <span class="ow">in</span> <span class="n">the</span> <span class="k">except</span> <span class="n">block</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">pop</span> <span class="n">the</span> <span class="n">current</span> <span class="n">stack</span> <span class="n">frame</span> <span class="n">off</span> <span class="n">the</span> <span class="n">call</span> <span class="n">stack</span>
<span class="c1"># Call stack is now empty</span>
<span class="nb">print</span> <span class="n">traceback</span> <span class="ow">and</span> <span class="n">exit</span>
</pre></div>
</div>
</div>
</section>
</section>
<section id="exceptions-are-not-always-errors">
<h2><span class="section-number">6.6. </span>Exceptions are not always errors<a class="headerlink" href="#exceptions-are-not-always-errors" title="Link to this heading">¶</a></h2>
<p>This chapter is called “Errors and exceptions”, so it is appropriate
to finish by drawing attention to the distinction between these two
concepts. While user errors and bugs in programs typically result in
an exception being raised, it is not the case that all exceptions
result from errors. The name “exception” means what it says, it is an
event whose occurrence requires an exception to the normal sequence of
execution.</p>
<p>The <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#StopIteration" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">StopIteration</span></code></a> exception which we encountered in
<a class="reference internal" href="5_abstract_data_types.html#iterator-protocol"><span class="std std-numref">Section 5.6</span></a> is a good example of an <a class="reference internal" href="#term-exception"><span class="xref std std-term">exception</span></a>
which does not indicate an error. The end of the set of things to be
iterated over does not indicate that something has gone wrong, but it
is an exception to the usual behaviour of <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#iterator.__next__" title="(in Python v3.14)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a>,
which Python needs to handle in a different way from simply returning
the next item.</p>
</section>
<section id="glossary">
<h2><span class="section-number">6.7. </span>Glossary<a class="headerlink" href="#glossary" title="Link to this heading">¶</a></h2>
<blockquote>
<div><dl class="simple glossary">
<dt id="term-call-stack">call stack<a class="headerlink" href="#term-call-stack" title="Link to this term">¶</a></dt><dt id="term-execution-stack">execution stack<a class="headerlink" href="#term-execution-stack" title="Link to this term">¶</a></dt><dt id="term-interpreter-stack">interpreter stack<a class="headerlink" href="#term-interpreter-stack" title="Link to this term">¶</a></dt><dd><p>The <a class="reference internal" href="5_abstract_data_types.html#term-stack"><span class="xref std std-term">stack</span></a> of <a class="reference internal" href="#term-stack-frame"><span class="xref std std-term">stack frames</span></a> in existence. The
current item on the stack is the currently executing function,
while the deepest item is the stack frame corresponding to the
user script or interpreter.</p>
</dd>
<dt id="term-exception">exception<a class="headerlink" href="#term-exception" title="Link to this term">¶</a></dt><dd><p>An object representing an out of the ordinary event which has
occurred during the execution of some Python code. When an
exception is <a class="reference internal" href="#raising-exceptions"><span class="std std-ref">raised</span></a> the
<a class="reference internal" href="2_programs_in_files.html#term-Python-interpreter"><span class="xref std std-term">Python interpreter</span></a> doesn’t continue to execute the
following line of code. Instead, the exception is either
<a class="reference internal" href="#handling-exceptions"><span class="std std-ref">handled</span></a> or execution stops and a
<a class="reference internal" href="#term-traceback"><span class="xref std std-term">traceback</span></a> is printed.</p>
</dd>
<dt id="term-stack-frame">stack frame<a class="headerlink" href="#term-stack-frame" title="Link to this term">¶</a></dt><dd><p>An object encapsulating the set of variables which define the
execution of a Python script or function. This information
includes the code being executed, all the local and global
names which are visible, the last instruction that was
executed, and a reference to the stack frame which called this
function.</p>
</dd>
<dt id="term-syntax">syntax<a class="headerlink" href="#term-syntax" title="Link to this term">¶</a></dt><dd><p>The set of rules which define what is a well-formed Python
statement. For example the rule that statements which start
blocks must end with a colon (:) is a syntax rule.</p>
</dd>
<dt id="term-syntax-error">syntax error<a class="headerlink" href="#term-syntax-error" title="Link to this term">¶</a></dt><dd><p>The <a class="reference internal" href="#term-exception"><span class="xref std std-term">exception</span></a> which occurs when a statement violates
the <a class="reference internal" href="#term-syntax"><span class="xref std std-term">syntax</span></a> rules of Python. Mismatched brackets,
missing commas, and incorrect indentation are all examples of
syntax errors.</p>
</dd>
<dt id="term-traceback">traceback<a class="headerlink" href="#term-traceback" title="Link to this term">¶</a></dt><dt id="term-stack-trace">stack trace<a class="headerlink" href="#term-stack-trace" title="Link to this term">¶</a></dt><dt id="term-back-trace">back trace<a class="headerlink" href="#term-back-trace" title="Link to this term">¶</a></dt><dd><p>A text representation of the <a class="reference internal" href="#term-call-stack"><span class="xref std std-term">call stack</span></a>. A traceback
shows a few lines of code around the current execution point
in each <a class="reference internal" href="#term-stack-frame"><span class="xref std std-term">stack frame</span></a>, with the current frame at the
bottom and the outermost frame at the top.</p>
</dd>
</dl>
</div></blockquote>
</section>
<section id="exercises">
<h2><span class="section-number">6.8. </span>Exercises<a class="headerlink" href="#exercises" title="Link to this heading">¶</a></h2>
<p>Using the information on the <a class="reference external" href="https://object-oriented-python.github.io/edition3/exercises.html">book website</a>
obtain the skeleton code for these exercises.</p>
<div class="proof proof-type-exercise" id="id11">
<div class="proof-title">
<span class="proof-type">Exercise 6.1</span>
</div><div class="proof-content">
<p>The Newton-Raphson method is an iterative method for approximately solving
equations of the form <span class="math notranslate nohighlight">\(f(x)=0\)</span>. Starting from an initial guess, a
series of (hopefully convergent) approximations to the solution is computed:</p>
<div class="math notranslate nohighlight">
\[x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}\]</div>
<p>The iteration concludes successfully if <span class="math notranslate nohighlight">\(|f(x_{n+1})| < \epsilon\)</span> for some
user-specified tolerance <span class="math notranslate nohighlight">\(\epsilon>0\)</span>. The sequence is not guaranteed
to converge for all combinations of function and starting point, so the
iteration should fail if <span class="math notranslate nohighlight">\(n\)</span> exceeds a user-specified number of
iterations.</p>
<p>The skeleton code for this chapter contains a function
<code class="xref py py-func docutils literal notranslate"><span class="pre">nonlinear_solvers.solvers.newton_raphson()</span></code> which takes as arguments a
function, its derivative and a starting point for the iteration. It can also
optionally be passed a value for <span class="math notranslate nohighlight">\(\epsilon\)</span> and a maximum number of
iterations to execute. Implement this function. If the iteration succeeds
then the last iterate, <span class="math notranslate nohighlight">\(x_{n+1}\)</span>, should be returned.</p>
<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">nonlinear_solvers.solvers</span></code> also defines an exception,
<code class="xref py py-class docutils literal notranslate"><span class="pre">ConvergenceError</span></code>. If the Newton-Raphson iteration exceeds the
number of iterations allowed then this exception should be raised, with an
appropriate error message.</p>
</div></div><div class="proof proof-type-exercise" id="id12">
<div class="proof-title">
<span class="proof-type">Exercise 6.2</span>
</div><div class="proof-content">
<p>The bisection method is a slower but more robust iterative solver. It requires a
function <span class="math notranslate nohighlight">\(f\)</span> and two starting points <span class="math notranslate nohighlight">\(x_0\)</span> and <span class="math notranslate nohighlight">\(x_1\)</span> such
that <span class="math notranslate nohighlight">\(f(x_0)\)</span> and <span class="math notranslate nohighlight">\(f(x_1)\)</span> differ in sign. At each stage of the
iteration, the function is evaluated at the midpoint of the current points
<span class="math notranslate nohighlight">\(x^* = (x_0 + x_1)/2\)</span>. If <span class="math notranslate nohighlight">\(|\,f(x^*)|<\epsilon\)</span> then the iteration
terminates successfully. Otherwise, <span class="math notranslate nohighlight">\(x^*\)</span> replaces <span class="math notranslate nohighlight">\(x_0\)</span> if
<span class="math notranslate nohighlight">\(f(x_0)\)</span> and <span class="math notranslate nohighlight">\(f(x^*)\)</span> have the same sign, and replaces
<span class="math notranslate nohighlight">\(x_1\)</span> otherwise.</p>
<p>Implement <code class="xref py py-func docutils literal notranslate"><span class="pre">nonlinear_solvers.solvers.bisection()</span></code>. As before, if the
iteration succeeds then return the last value of <span class="math notranslate nohighlight">\(x\)</span>. If the maximum
number of iterations is exceeded, raise <code class="xref py py-class docutils literal notranslate"><span class="pre">ConvergenceError</span></code> with a
suitable error message. The bisection method has a further failure mode. If
<span class="math notranslate nohighlight">\(f(x_0)\)</span> and <span class="math notranslate nohighlight">\(f(x_1)\)</span> do not differ in sign then your code
should raise <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ValueError" title="(in Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">ValueError</span></code></a> with a suitable message.</p>
</div></div><div class="proof proof-type-exercise" id="id13">
<div class="proof-title">
<span class="proof-type">Exercise 6.3</span>
</div><div class="proof-content">
<p>Implement the function <code class="xref py py-func docutils literal notranslate"><span class="pre">nonlinear_solvers.solvers.solve()</span></code>. This code
should first attempt to solve <span class="math notranslate nohighlight">\(f(x)=0\)</span> using your Newton-Raphson
function. If that fails it should catch the exception and instead try using
your bisection function.</p>
</div></div><p class="rubric">Footnotes</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="tut-exceptions" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id5">1</a><span class="fn-bracket">]</span></span>
<p><a class="reference external" href="https://docs.python.org/3/tutorial/errors.html#tut-handling">https://docs.python.org/3/tutorial/errors.html#tut-handling</a></p>
</aside>
<aside class="footnote brackets" id="function" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id2">2</a><span class="fn-bracket">]</span></span>
<p>“Function call” here includes <a class="reference internal" href="3_objects.html#term-method"><span class="xref std std-term">method</span></a> calls and
operations implemented using a <a class="reference internal" href="3_objects.html#term-special-method"><span class="xref std std-term">special method</span></a>.</p>
</aside>
<aside class="footnote brackets" id="exercise-page" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id6">3</a><span class="fn-bracket">]</span></span>
<p><a class="reference external" href="https://object-oriented-python.github.io/edition3/exercises.html">https://object-oriented-python.github.io/edition3/exercises.html</a></p>
</aside>
</aside>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
© Copyright 2019-2023, David A. Ham.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.4.7.
</div>
</body>
</html>