-
Notifications
You must be signed in to change notification settings - Fork 3
/
readme.html
102 lines (102 loc) · 3.44 KB
/
readme.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=yes"
/>
<title>readme</title>
<style type="text/css">
code {
white-space: pre-wrap;
}
span.smallcaps {
font-variant: small-caps;
}
span.underline {
text-decoration: underline;
}
div.column {
display: inline-block;
vertical-align: top;
width: 50%;
}
</style>
</head>
<body>
<h1 id="practice-exercises-for-error-handling">
Practice Exercises for error handling
</h1>
<h2 id="overview">Overview</h2>
<p>
In this section, you’ll have a chance to practice the concepts you’ve
learned in the videos. First, review the core concepts covered that you’ll
need to keep in mind. Then go through the exercises below.
</p>
<p>
Remember, these are for your own benefit. Feel free to skip them if you
don’t find a particular exercise valuable or you get stuck for too long.
</p>
<h2 id="core-concepts">Core concepts</h2>
<h3 id="try-except">try / except</h3>
<p>
When handling errors, we can check for bad values (e.g. <code>None</code>
where a proper string was expected). But Python’s native error handling
approach is exception-based: throwing and catching exceptions.
</p>
<p>Below is the minimum code to catch an error in Python.</p>
<pre><code>try:
do_risky_thing1()
do_risky_thing2()
do_risky_thing3()
except Exception as x:
# Deal with error, use x for help on what happened.</code></pre>
<h3 id="multiple-error-types">Multiple error types</h3>
<p>
The example above is good to catch errors. But it catches them all (well,
almost all of them), and it treats them all the same.
</p>
<p>
Below is code needed to handle different errors as well as unforeseen
errors.
</p>
<pre><code>try:
do_risky_thing1()
do_risky_thing2()
do_risky_thing3()
except json.decoder.JSONDecodeError:
# Handle malformed JSON error
except FileNotFoundError as fe:
# Handle missing file error
except ValueError:
# Handle conversion error.
except Exception as x:
# Deal with error, use x for help on what happened.</code></pre>
<p>
<strong>Note</strong>: It is important that the most specific errors are
listed first and the most general the last (<code>Exception</code>).
Python selects the first (not best) match.
</p>
<h2 id="exercises">Exercises</h2>
<p>
Now it’s your turn. In this practice, go back to the tic tac toe game we
created back in the chapter on problem solving. Alternatively, if you made
it through Connect 4, you can work with that one instead. Your job will be
to:
</p>
<ul>
<li>
Add error handling around the input processing in your program using try
/ except. Be as specific on the errors as possible. If you can determine
the cause or type of error, give a specific message.
</li>
<li>
Think of all other ways your app could crash or run into trouble. Get it
to crash, check the error type in the traceback, and add the appropriate
error handling for this as well.
</li>
</ul>
</body>
</html>