|
61 | 61 |
|
62 | 62 | <div class="section" id="errors-and-exceptions"> |
63 | 63 | <span id="id1"></span><h1><span class="section-number">6. </span>Errors and exceptions<a class="headerlink" href="#errors-and-exceptions" title="Permalink to this headline">¶</a></h1> |
64 | | -<p>It is a sight familiar to every programmer: instead of producing the |
| 64 | +<details class="sphinx-bs dropdown card mb-3"> |
| 65 | +<summary class="summary-title card-header"> |
| 66 | +Video: errors and exceptions.<div class="summary-down docutils"> |
| 67 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 68 | +<div class="summary-up docutils"> |
| 69 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 70 | +</summary><div class="summary-content card-body docutils"> |
| 71 | +<div class="vimeo docutils container"> |
| 72 | +<iframe src="https://player.vimeo.com/video/509280820" |
| 73 | +frameborder="0" allow="autoplay; fullscreen" |
| 74 | +allowfullscreen></iframe></div> |
| 75 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=d58d4c55-6216-4deb-be70-acc7015033f4">watch this video on Panopto</a>.</p> |
| 76 | +</div> |
| 77 | +</details><p>It is a sight familiar to every programmer: instead of producing the |
65 | 78 | desired result, the screen is filled with seemingly unintelligible |
66 | 79 | garbage because an error has occurred. Producing errors is an |
67 | 80 | unavoidable part of programming, so learning to understand and correct |
@@ -204,7 +217,20 @@ <h2><span class="section-number">6.2. </span>Exceptions<a class="headerlink" hre |
204 | 217 | </div> |
205 | 218 | <div class="section" id="tracebacks-finding-errors"> |
206 | 219 | <h2><span class="section-number">6.3. </span>Tracebacks: finding errors<a class="headerlink" href="#tracebacks-finding-errors" title="Permalink to this headline">¶</a></h2> |
207 | | -<p>The errors we have looked at so far have all been located in the top |
| 220 | +<details class="sphinx-bs dropdown card mb-3"> |
| 221 | +<summary class="summary-title card-header"> |
| 222 | +Video: tracebacks.<div class="summary-down docutils"> |
| 223 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 224 | +<div class="summary-up docutils"> |
| 225 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 226 | +</summary><div class="summary-content card-body docutils"> |
| 227 | +<div class="vimeo docutils container"> |
| 228 | +<iframe src="https://player.vimeo.com/video/509280880" |
| 229 | +frameborder="0" allow="autoplay; fullscreen" |
| 230 | +allowfullscreen></iframe></div> |
| 231 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=f3f8a555-31c8-41e3-a176-acc701503469">watch this video on Panopto</a>.</p> |
| 232 | +</div> |
| 233 | +</details><p>The errors we have looked at so far have all been located in the top |
208 | 234 | level of code either typed directly into iPython or executed in a |
209 | 235 | script. However, what happens if an error occurs in a function call or |
210 | 236 | even several functions down? Consider the following code, which uses |
@@ -256,7 +282,20 @@ <h2><span class="section-number">6.3. </span>Tracebacks: finding errors<a class= |
256 | 282 | the call stack.</p> |
257 | 283 | <div class="section" id="the-call-stack"> |
258 | 284 | <h3><span class="section-number">6.3.1. </span>The call stack<a class="headerlink" href="#the-call-stack" title="Permalink to this headline">¶</a></h3> |
259 | | -<p>A Python program is a sequence of Python statements, which are |
| 285 | +<details class="sphinx-bs dropdown card mb-3"> |
| 286 | +<summary class="summary-title card-header"> |
| 287 | +Video: the call stack.<div class="summary-down docutils"> |
| 288 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 289 | +<div class="summary-up docutils"> |
| 290 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 291 | +</summary><div class="summary-content card-body docutils"> |
| 292 | +<div class="vimeo docutils container"> |
| 293 | +<iframe src="https://player.vimeo.com/video/509281576" |
| 294 | +frameborder="0" allow="autoplay; fullscreen" |
| 295 | +allowfullscreen></iframe></div> |
| 296 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=cab860f1-ff35-4402-afe9-acc701503419">watch this video on Panopto</a>.</p> |
| 297 | +</div> |
| 298 | +</details><p>A Python program is a sequence of Python statements, which are |
260 | 299 | executed in a sequence determined by the flow control logic of the |
261 | 300 | program itself. Each statement contains zero or more function calls <a class="footnote-reference brackets" href="#function" id="id2">1</a>, |
262 | 301 | which are executed in the course of evaluating that statement.</p> |
@@ -287,10 +326,6 @@ <h3><span class="section-number">6.3.1. </span>The call stack<a class="headerlin |
287 | 326 | functions which call functions and so on in a nearly limitless |
288 | 327 | sequence, there can be a number of stack frames in existence at any |
289 | 328 | time.</p> |
290 | | -<div class="admonition note"> |
291 | | -<p class="admonition-title">Note</p> |
292 | | -<p>FIXME: Do a video using the XCode debugger to show the call stack.</p> |
293 | | -</div> |
294 | 329 | </div> |
295 | 330 | <div class="section" id="interpreting-tracebacks"> |
296 | 331 | <h3><span class="section-number">6.3.2. </span>Interpreting tracebacks<a class="headerlink" href="#interpreting-tracebacks" title="Permalink to this headline">¶</a></h3> |
@@ -359,7 +394,20 @@ <h3><span class="section-number">6.3.2. </span>Interpreting tracebacks<a class=" |
359 | 394 | </div> |
360 | 395 | <div class="section" id="raising-exceptions"> |
361 | 396 | <span id="id3"></span><h2><span class="section-number">6.4. </span>Raising exceptions<a class="headerlink" href="#raising-exceptions" title="Permalink to this headline">¶</a></h2> |
362 | | -<p>Thus far we’ve noticed that an exception occurs when something goes |
| 397 | +<details class="sphinx-bs dropdown card mb-3"> |
| 398 | +<summary class="summary-title card-header"> |
| 399 | +Video: raising an exception.<div class="summary-down docutils"> |
| 400 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 401 | +<div class="summary-up docutils"> |
| 402 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 403 | +</summary><div class="summary-content card-body docutils"> |
| 404 | +<div class="vimeo docutils container"> |
| 405 | +<iframe src="https://player.vimeo.com/video/509492490" |
| 406 | +frameborder="0" allow="autoplay; fullscreen" |
| 407 | +allowfullscreen></iframe></div> |
| 408 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=d0b05710-bbb8-47b4-9afa-acc8011e7635">watch this video on Panopto</a>.</p> |
| 409 | +</div> |
| 410 | +</details><p>Thus far we’ve noticed that an exception occurs when something goes |
363 | 411 | 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 |
364 | 412 | 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 |
365 | 413 | process by which an exception occurs.</p> |
@@ -459,7 +507,20 @@ <h3><span class="section-number">6.3.2. </span>Interpreting tracebacks<a class=" |
459 | 507 | </div> |
460 | 508 | <div class="section" id="handling-exceptions"> |
461 | 509 | <span id="id4"></span><h2><span class="section-number">6.5. </span>Handling exceptions<a class="headerlink" href="#handling-exceptions" title="Permalink to this headline">¶</a></h2> |
462 | | -<p>So far we have seen several different sorts of exception, how to raise |
| 510 | +<details class="sphinx-bs dropdown card mb-3"> |
| 511 | +<summary class="summary-title card-header"> |
| 512 | +Video: handling exceptions.<div class="summary-down docutils"> |
| 513 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 514 | +<div class="summary-up docutils"> |
| 515 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 516 | +</summary><div class="summary-content card-body docutils"> |
| 517 | +<div class="vimeo docutils container"> |
| 518 | +<iframe src="https://player.vimeo.com/video/509492495" |
| 519 | +frameborder="0" allow="autoplay; fullscreen" |
| 520 | +allowfullscreen></iframe></div> |
| 521 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=25f14034-34a1-44ec-83f7-acc8011e76a0">watch this video on Panopto</a>.</p> |
| 522 | +</div> |
| 523 | +</details><p>So far we have seen several different sorts of exception, how to raise |
463 | 524 | them, and how to understand the resulting <a class="reference internal" href="#term-traceback"><span class="xref std std-term">traceback</span></a>. The |
464 | 525 | <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 |
465 | 526 | in our code, so that we need to understand and correct the |
@@ -542,7 +603,20 @@ <h3><span class="section-number">6.3.2. </span>Interpreting tracebacks<a class=" |
542 | 603 | </div> |
543 | 604 | <div class="section" id="except-clauses"> |
544 | 605 | <h3><span class="section-number">6.5.1. </span>Except clauses<a class="headerlink" href="#except-clauses" title="Permalink to this headline">¶</a></h3> |
545 | | -<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.9)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> works. The full |
| 606 | +<details class="sphinx-bs dropdown card mb-3"> |
| 607 | +<summary class="summary-title card-header"> |
| 608 | +Video: further exception handling.<div class="summary-down docutils"> |
| 609 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 610 | +<div class="summary-up docutils"> |
| 611 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 612 | +</summary><div class="summary-content card-body docutils"> |
| 613 | +<div class="vimeo docutils container"> |
| 614 | +<iframe src="https://player.vimeo.com/video/509492496" |
| 615 | +frameborder="0" allow="autoplay; fullscreen" |
| 616 | +allowfullscreen></iframe></div> |
| 617 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=0d7840de-17b2-4268-b079-acc8011e7660">watch this video on Panopto</a>.</p> |
| 618 | +</div> |
| 619 | +</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.9)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> works. The full |
546 | 620 | version of the except statement takes a tuple of exception classes. If an |
547 | 621 | exception is raised matching any of the exceptions in that tuple then the code |
548 | 622 | in the except block is executed.</p> |
|
0 commit comments