Skip to content

Commit 38e82ee

Browse files
committed
Exceptions
1 parent 8a1efeb commit 38e82ee

File tree

2 files changed

+105
-42
lines changed

2 files changed

+105
-42
lines changed

README.md

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,36 +1306,63 @@ from functools import partial
13061306
LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r),
13071307
'OR' : partial(lambda l, r: l or r)})
13081308
```
1309+
* **Another solution in this particular case, is to use `'and_'` and `'or_'` functions from module [operator](#operator) as values.**
13091310

13101311

13111312
Exceptions
13121313
----------
1314+
1315+
### Basic Example
13131316
```python
1314-
while True:
1315-
try:
1316-
x = int(input('Please enter a number: '))
1317-
except ValueError:
1318-
print('Oops! That was no valid number. Try again...')
1319-
else:
1320-
print('Thank you.')
1321-
break
1317+
try:
1318+
<code>
1319+
except <exception>:
1320+
<code>
1321+
```
1322+
1323+
### Complex Example
1324+
```python
1325+
try:
1326+
<code_1>
1327+
except <exception_a>:
1328+
<code_2_a>
1329+
except <exception_b>:
1330+
<code_2_b>
1331+
else:
1332+
<code_2_c>
1333+
finally:
1334+
<code_3>
1335+
```
1336+
1337+
### Catching Exceptions
1338+
```python
1339+
except <exception>:
1340+
except <exception> as <name>:
1341+
except (<exception_1>, <exception_2>, ...):
1342+
except (<exception_1>, <exception_2>, ...) as <name>:
1343+
```
1344+
* **Also catches subclasses of the exception.**
1345+
1346+
### Raising Exceptions
1347+
```python
1348+
raise <exception>
1349+
raise <exception>()
1350+
raise <exception>(<el>)
1351+
raise <exception>(<el_1>, <el_2>, ...)
13221352
```
13231353

1324-
### Raising Exception
1354+
#### Useful built-in exceptions:
13251355
```python
1326-
raise ValueError('A very specific message!')
1356+
raise ValueError('Argument is of right type but inappropriate value!')
1357+
raise TypeError('Argument is of wrong type!')
1358+
raise RuntimeError('None of above!')
13271359
```
13281360

1329-
### Finally
1361+
#### Re-raising caught exception:
13301362
```python
1331-
>>> try:
1332-
... raise KeyboardInterrupt
1333-
... finally:
1334-
... print('Goodbye, world!')
1335-
Goodbye, world!
1336-
Traceback (most recent call last):
1337-
File "<stdin>", line 2, in <module>
1338-
KeyboardInterrupt
1363+
except <exception>:
1364+
<code>
1365+
raise
13391366
```
13401367

13411368
### Common Built-in Exceptions
@@ -1361,7 +1388,15 @@ BaseException
13611388
+-- ValueError # When an argument is of right type but inappropriate value.
13621389
+-- UnicodeError # Raised when encoding/decoding strings from/to bytes fails.
13631390
```
1364-
* **Appropriate built-in exceptions for user to rise are: 'ValueError', 'TypeError' and 'RuntimeError'.**
1391+
1392+
### User-defined Exceptions
1393+
```python
1394+
class MyError(Exception):
1395+
pass
1396+
1397+
class MyInputError(MyError):
1398+
pass
1399+
```
13651400

13661401

13671402
Print

index.html

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,28 +1210,52 @@ <h4 id="functionscannotbevaluessotheymustbewrapped">Functions can not be values,
12101210
LogicOp = Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">and</span> r),
12111211
<span class="hljs-string">'OR'</span> : partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">or</span> r)})
12121212
</code></pre>
1213+
<ul>
1214+
<li><strong>Another solution in this particular case, is to use <code class="python hljs"><span class="hljs-string">'and_'</span></code> and <code class="python hljs"><span class="hljs-string">'or_'</span></code> functions from module <a href="#operator">operator</a> as values.</strong></li>
1215+
</ul>
12131216
<h2 id="exceptions"><a href="#exceptions" name="exceptions">#</a>Exceptions</h2>
1214-
<pre><code class="python language-python hljs"><span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
1215-
<span class="hljs-keyword">try</span>:
1216-
x = int(input(<span class="hljs-string">'Please enter a number: '</span>))
1217-
<span class="hljs-keyword">except</span> ValueError:
1218-
print(<span class="hljs-string">'Oops! That was no valid number. Try again...'</span>)
1219-
<span class="hljs-keyword">else</span>:
1220-
print(<span class="hljs-string">'Thank you.'</span>)
1221-
<span class="hljs-keyword">break</span>
1217+
<h3 id="basicexample">Basic Example</h3>
1218+
<pre><code class="python language-python hljs"><span class="hljs-keyword">try</span>:
1219+
&lt;code&gt;
1220+
<span class="hljs-keyword">except</span> &lt;exception&gt;:
1221+
&lt;code&gt;
12221222
</code></pre>
1223-
<h3 id="raisingexception">Raising Exception</h3>
1224-
<pre><code class="python language-python hljs"><span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">'A very specific message!'</span>)
1223+
<h3 id="complexexample">Complex Example</h3>
1224+
<pre><code class="python language-python hljs"><span class="hljs-keyword">try</span>:
1225+
&lt;code_1&gt;
1226+
<span class="hljs-keyword">except</span> &lt;exception_a&gt;:
1227+
&lt;code_2_a&gt;
1228+
<span class="hljs-keyword">except</span> &lt;exception_b&gt;:
1229+
&lt;code_2_b&gt;
1230+
<span class="hljs-keyword">else</span>:
1231+
&lt;code_2_c&gt;
1232+
<span class="hljs-keyword">finally</span>:
1233+
&lt;code_3&gt;
1234+
</code></pre>
1235+
<h3 id="catchingexceptions">Catching Exceptions</h3>
1236+
<pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> &lt;exception&gt;:
1237+
<span class="hljs-keyword">except</span> &lt;exception&gt; <span class="hljs-keyword">as</span> &lt;name&gt;:
1238+
<span class="hljs-keyword">except</span> (&lt;exception_1&gt;, &lt;exception_2&gt;, ...):
1239+
<span class="hljs-keyword">except</span> (&lt;exception_1&gt;, &lt;exception_2&gt;, ...) <span class="hljs-keyword">as</span> &lt;name&gt;:
12251240
</code></pre>
1226-
<h3 id="finally">Finally</h3>
1227-
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">try</span>:
1228-
<span class="hljs-meta">... </span> <span class="hljs-keyword">raise</span> KeyboardInterrupt
1229-
<span class="hljs-meta">... </span><span class="hljs-keyword">finally</span>:
1230-
<span class="hljs-meta">... </span> print(<span class="hljs-string">'Goodbye, world!'</span>)
1231-
Goodbye, world!
1232-
Traceback (most recent call last):
1233-
File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">2</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
1234-
KeyboardInterrupt
1241+
<ul>
1242+
<li><strong>Also catches subclasses of the exception.</strong></li>
1243+
</ul>
1244+
<h3 id="raisingexceptions">Raising Exceptions</h3>
1245+
<pre><code class="python language-python hljs"><span class="hljs-keyword">raise</span> &lt;exception&gt;
1246+
<span class="hljs-keyword">raise</span> &lt;exception&gt;()
1247+
<span class="hljs-keyword">raise</span> &lt;exception&gt;(&lt;el&gt;)
1248+
<span class="hljs-keyword">raise</span> &lt;exception&gt;(&lt;el_1&gt;, &lt;el_2&gt;, ...)
1249+
</code></pre>
1250+
<h4 id="usefulbuiltinexceptions">Useful built-in exceptions:</h4>
1251+
<pre><code class="python language-python hljs"><span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">'Argument is of right type but inappropriate value!'</span>)
1252+
<span class="hljs-keyword">raise</span> TypeError(<span class="hljs-string">'Argument is of wrong type!'</span>)
1253+
<span class="hljs-keyword">raise</span> RuntimeError(<span class="hljs-string">'None of above!'</span>)
1254+
</code></pre>
1255+
<h4 id="reraisingcaughtexception">Re-raising caught exception:</h4>
1256+
<pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> &lt;exception&gt;:
1257+
&lt;code&gt;
1258+
<span class="hljs-keyword">raise</span>
12351259
</code></pre>
12361260
<h3 id="commonbuiltinexceptions">Common Built-in Exceptions</h3>
12371261
<pre><code class="python language-python hljs">BaseException
@@ -1255,9 +1279,13 @@ <h3 id="commonbuiltinexceptions">Common Built-in Exceptions</h3>
12551279
+-- ValueError <span class="hljs-comment"># When an argument is of right type but inappropriate value.</span>
12561280
+-- UnicodeError <span class="hljs-comment"># Raised when encoding/decoding strings from/to bytes fails. </span>
12571281
</code></pre>
1258-
<ul>
1259-
<li><strong>Appropriate built-in exceptions for user to rise are: 'ValueError', 'TypeError' and 'RuntimeError'.</strong></li>
1260-
</ul>
1282+
<h3 id="userdefinedexceptions">User-defined Exceptions</h3>
1283+
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyError</span><span class="hljs-params">(Exception)</span>:</span>
1284+
<span class="hljs-keyword">pass</span>
1285+
1286+
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyInputError</span><span class="hljs-params">(MyError)</span>:</span>
1287+
<span class="hljs-keyword">pass</span>
1288+
</code></pre>
12611289
<h2 id="print"><a href="#print" name="print">#</a>Print</h2>
12621290
<pre><code class="python language-python hljs">print(&lt;el_1&gt;, ..., sep=<span class="hljs-string">' '</span>, end=<span class="hljs-string">'\n'</span>, file=sys.stdout, flush=<span class="hljs-keyword">False</span>)
12631291
</code></pre>

0 commit comments

Comments
 (0)