https://www.ocf.berkeley.edu/~shidi/cs61a/w/index.php?title=Exception&feed=atom&action=historyException - Revision history2024-03-29T02:16:54ZRevision history for this page on the wikiMediaWiki 1.22.6https://www.ocf.berkeley.edu/~shidi/cs61a/w/index.php?title=Exception&diff=973&oldid=prevAxis: {{Sufficient-class}}2014-07-24T23:51:27Z<p>{{Sufficient-class}}</p>
<table class='diff diff-contentalign-left'>
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr style='vertical-align: top;'>
<td colspan='2' style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan='2' style="background-color: white; color:black; text-align: center;">Revision as of 23:51, 24 July 2014</td>
</tr><tr><td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td></tr>
<tr><td class='diff-marker'>−</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>An '''exception''' is an object that represents an error.<ref>https://docs.google.com/presentation/d/1j_a3kWQwnqyFRo5ud1iJUzVKPK37JkQna9_m9h2u7O0/edit#slide=id.g38ed26997_126</ref> Exceptions are ''raised'' either automatically by Python or explicitly by the programmer.</div></td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins class="diffchange diffchange-inline">{{Sufficient-class}}</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>An '''exception''' is an <ins class="diffchange diffchange-inline">[[</ins>object<ins class="diffchange diffchange-inline">]] </ins>that represents an error.<ref>https://docs.google.com/presentation/d/1j_a3kWQwnqyFRo5ud1iJUzVKPK37JkQna9_m9h2u7O0/edit#slide=id.g38ed26997_126</ref> Exceptions are ''raised'' either automatically by Python or explicitly by the programmer.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>This is a full list of exceptions: https://docs.python.org/3/library/exceptions.html#bltin-exceptions.</div></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>This is a full list of exceptions: https://docs.python.org/3/library/exceptions.html#bltin-exceptions.</div></td></tr>
</table>Axishttps://www.ocf.berkeley.edu/~shidi/cs61a/w/index.php?title=Exception&diff=970&oldid=prevAxis: created2014-07-24T23:47:44Z<p>created</p>
<p><b>New page</b></p><div>An '''exception''' is an object that represents an error.<ref>https://docs.google.com/presentation/d/1j_a3kWQwnqyFRo5ud1iJUzVKPK37JkQna9_m9h2u7O0/edit#slide=id.g38ed26997_126</ref> Exceptions are ''raised'' either automatically by Python or explicitly by the programmer.<br />
<br />
This is a full list of exceptions: https://docs.python.org/3/library/exceptions.html#bltin-exceptions.<br />
<br />
== Types ==<br />
There are two different types of exceptions: ones that happen when the program is being parsed by Python (<code>SyntaxError</code>, <code>IndentationError</code>), and ones that happen when the program is running (runtime exceptions).<br />
<br />
== Raising an exception ==<br />
Use the <code>raise</code> statement. Example:<br />
<syntaxhighlight lang="python"><br />
>>> while True:<br />
... num = int(input("Enter a number between 1 and 10:\n"))<br />
... if not 1 <= num <= 10:<br />
... raise ValueError("Number out of range")<br />
... <br />
Enter a number between 1 and 10:<br />
5<br />
Enter a number between 1 and 10:<br />
0<br />
Traceback (most recent call last):<br />
...<br />
ValueError: Number out of range<br />
</syntaxhighlight><br />
<br />
== Handling an exception ==<br />
Runtime exceptions can be caught. We use the <code>try</code>-<code>except</code> construct to handle an exception. If an error is encountered in the <code>try</code> block, execution stops and is transferred to the except block. <br />
<br />
Example:<br />
<syntaxhighlight lang="python"><br />
>>> try:<br />
... print(1/0)<br />
... except ZeroDivisionError:<br />
... print("Can't divide by zero!")<br />
... <br />
Can't divide by zero!<br />
</syntaxhighlight><br />
<br />
There can be multiple <code>except</code> clauses for different types of exceptions. An <code>except</code> clause can also not have an exception specified, in which case it would handle all runtime exceptions. Example:<br />
<syntaxhighlight lang="python"><br />
>>> try:<br />
... lst = None<br />
... lst.append(2)<br />
... except:<br />
... print("Something bad happened!")<br />
... <br />
Something bad happened!<br />
</syntaxhighlight><br />
<br />
== Sources ==<br />
<references></div>Axis