forked from rachelslaybaugh/python-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-exceptions.html
More file actions
108 lines (108 loc) · 7 KB
/
03-exceptions.html
File metadata and controls
108 lines (108 loc) · 7 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Testing</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">Testing</h1></a>
<h2 class="subtitle">Exceptions</h2>
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>Understand that exceptions are effectively specialized runtime tests</li>
<li>Learn when to use exceptions and what exceptions are available</li>
</ul>
</div>
</section>
<p>Exceptions are more sophisticated than assertions. They are the standard error messaging system in most modern programming languages. Fundamentally, when an error is encountered, an informative exception is ‘thrown’ or ‘raised’.</p>
<p>For example, instead of the assertion in the case before, an exception can be used.</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list):
<span class="kw">if</span> <span class="dt">len</span>(num_list) == <span class="dv">0</span> :
<span class="kw">raise</span> <span class="ot">Exception</span>(<span class="st">"The algebraic mean of an empty list is undefined. Please provide a list of numbers"</span>)
<span class="kw">else</span> :
<span class="kw">return</span> <span class="dt">sum</span>(num_list)/<span class="dt">len</span>(num_list)</code></pre>
<p>Once an exception is raised, it will be passed upward in the program scope. An exception be used to trigger additional error messages or an alternative behavior. rather than immediately halting code execution, the exception can be ‘caught’ upstream with a try-except block. When wrapped in a try-except block, the exception can be intercepted before it reaches global scope and halts execution.</p>
<p>To add information or replace the message before it is passed upstream, the try-catch block can be used to catch-and-reraise the exception:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list):
<span class="kw">try</span>:
<span class="kw">return</span> <span class="dt">sum</span>(num_list)/<span class="dt">len</span>(num_list)
<span class="kw">except</span> <span class="ot">ZeroDivisionError</span> <span class="ch">as</span> detail :
msg = <span class="st">"The algebraic mean of an empty list is undefined. Please provide a list of numbers."</span>
<span class="kw">raise</span> <span class="ot">ZeroDivisionError</span>(detail.<span class="ot">__str__</span>() + <span class="st">"</span><span class="ch">\n</span><span class="st">"</span> + msg)</code></pre>
<p>Alternatively, the exception can simply be handled intelligently. If an alternative behavior is preferred, the exception can be disregarded and a responsive behavior can be implemented like so:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list):
<span class="kw">try</span>:
<span class="kw">return</span> <span class="dt">sum</span>(num_list)/<span class="dt">len</span>(num_list)
<span class="kw">except</span> <span class="ot">ZeroDivisionError</span> :
<span class="kw">return</span> <span class="dv">0</span></code></pre>
<p>If a single function might raise more than one type of exception, each can be caught and handled separately.</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list):
<span class="kw">try</span>:
<span class="kw">return</span> <span class="dt">sum</span>(num_list)/<span class="dt">len</span>(num_list)
<span class="kw">except</span> <span class="ot">ZeroDivisionError</span> :
<span class="kw">return</span> <span class="dv">0</span>
<span class="kw">except</span> <span class="ot">TypeError</span> <span class="ch">as</span> detail :
msg = <span class="st">"The algebraic mean of an non-numerical list is undefined. Please provide a list of numbers."</span>
<span class="kw">raise</span> <span class="ot">TypeError</span>(detail.<span class="ot">__str__</span>() + <span class="st">"</span><span class="ch">\n</span><span class="st">"</span> + msg)</code></pre>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pencil"></span>What Else Can Go Wrong?</h2>
</div>
<div class="panel-body">
<ol style="list-style-type: decimal">
<li>Think of some other type of exception that could be raised by the try block.</li>
<li>Guard against it by adding an except clause.</li>
</ol>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pencil"></span>Cause All of the Errors</h2>
</div>
<div class="panel-body">
<ul>
<li>Use the mean function in three different ways, so that you cause each exceptional case.</li>
</ul>
</div>
</section>
<p>Exceptions have the advantage of being simple to include and powerfully helpful to the user. However, not all behaviors can or should be found with runtime exceptions. Most behaviors should be validated with unit tests.</p>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/lesson-template">Source</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
</body>
</html>