<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Exceptions_%28try_%2F_except%29</id>
	<title>Exceptions (try / except) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Exceptions_%28try_%2F_except%29"/>
	<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Exceptions_(try_/_except)&amp;action=history"/>
	<updated>2026-08-08T15:36:18Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Exceptions_(try_/_except)&amp;diff=330&amp;oldid=prev</id>
		<title>Davrot at 16:15, 17 October 2025</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Exceptions_(try_/_except)&amp;diff=330&amp;oldid=prev"/>
		<updated>2025-10-17T16:15:47Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:15, 17 October 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;== The goal ==&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The “modern” way (the concept is from the 1960) to deal with errors is to use exceptions. “Normal” errors just kill your program. Exceptions allow you to react to the error (if you want to). Plus, it is standardized and you don’t need to invent your own approach, which is important if source code is exchanged.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The “modern” way (the concept is from the 1960) to deal with errors is to use exceptions. “Normal” errors just kill your program. Exceptions allow you to react to the error (if you want to). Plus, it is standardized and you don’t need to invent your own approach, which is important if source code is exchanged.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l165&quot;&gt;Line 165:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 164:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Chaining exceptions ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Chaining exceptions ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{: .topic-optional} &lt;/del&gt;This is an optional topic!&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&#039;&#039;&#039;&lt;/ins&gt;This is an optional topic!&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&#039;&#039;&#039;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Instead of just re-raising an exception we can also add to it with &amp;#039;&amp;#039;&amp;#039;from&amp;#039;&amp;#039;&amp;#039;. &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def function() -&amp;gt; None:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Instead of just re-raising an exception we can also add to it with &amp;#039;&amp;#039;&amp;#039;from&amp;#039;&amp;#039;&amp;#039;. &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def function() -&amp;gt; None:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Exceptions_(try_/_except)&amp;diff=157&amp;oldid=prev</id>
		<title>Davrot: Created page with &quot; == The goal == The “modern” way (the concept is from the 1960) to deal with errors is to use exceptions. “Normal” errors just kill your program. Exceptions allow you to react to the error (if you want to). Plus, it is standardized and you don’t need to invent your own approach, which is important if source code is exchanged.  An exception is thrown (i.e. raised under Python). And the program has to catch and deal with the exception.  Questions to [mailto:davro...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Exceptions_(try_/_except)&amp;diff=157&amp;oldid=prev"/>
		<updated>2025-10-15T13:39:53Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; == The goal == The “modern” way (the concept is from the 1960) to deal with errors is to use exceptions. “Normal” errors just kill your program. Exceptions allow you to react to the error (if you want to). Plus, it is standardized and you don’t need to invent your own approach, which is important if source code is exchanged.  An exception is thrown (i.e. raised under Python). And the program has to catch and deal with the exception.  Questions to [mailto:davro...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
== The goal ==&lt;br /&gt;
The “modern” way (the concept is from the 1960) to deal with errors is to use exceptions. “Normal” errors just kill your program. Exceptions allow you to react to the error (if you want to). Plus, it is standardized and you don’t need to invent your own approach, which is important if source code is exchanged.&lt;br /&gt;
&lt;br /&gt;
An exception is thrown (i.e. raised under Python). And the program has to catch and deal with the exception.&lt;br /&gt;
&lt;br /&gt;
Questions to [mailto:davrot@uni-bremen.de David Rotermund]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/exceptions.html Notes:]&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Never use the BaseException for developing your own exception. &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;All user-defined exceptions should be derived from Exception.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The exception &amp;#039;&amp;#039;&amp;#039;KeyboardInterrupt&amp;#039;&amp;#039;&amp;#039; inherits directly from &amp;#039;&amp;#039;&amp;#039;BaseException&amp;#039;&amp;#039;&amp;#039; so as to not be accidentally caught by code that catches Exception and thus prevent the interpreter from exiting.&lt;br /&gt;
* The exception &amp;#039;&amp;#039;&amp;#039;SystemExit&amp;#039;&amp;#039;&amp;#039; inherits from &amp;#039;&amp;#039;&amp;#039;BaseException&amp;#039;&amp;#039;&amp;#039; instead of Exception so that it is not accidentally caught by code that catches Exception.&lt;br /&gt;
* Put as few commands as possible into the try section. (see [https://peps.python.org/pep-0008/#programming-recommendations PEP8 style guide])&lt;br /&gt;
* The most laziest exception handling should use Exception as target. &amp;#039;&amp;#039;&amp;#039;Never&amp;#039;&amp;#039;&amp;#039; use a bare except:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    [...]&lt;br /&gt;
except Exception:&lt;br /&gt;
    [...]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples of Exception ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;10 * (1 / 0) # -&amp;gt; ZeroDivisionError: division by zero&lt;br /&gt;
&lt;br /&gt;
4 * not_there_variable + 1 # -&amp;gt; NameError: name &amp;#039;not_there_variable&amp;#039; is not defined&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1&amp;quot; + 1 # -&amp;gt; TypeError: can only concatenate str (not &amp;quot;int&amp;quot;) to str&lt;br /&gt;
&lt;br /&gt;
with open(&amp;quot;file_that_is_not_there.nope&amp;quot;, &amp;quot;r&amp;quot;) as fid: # -&amp;gt; FileNotFoundError: [Errno 2] No such file or directory: &amp;#039;file_that_is_not_there.nope&amp;#039;&lt;br /&gt;
    pass&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/tutorial/errors.html#handling-exceptions How to deal with exceptions] ==&lt;br /&gt;
We can use [https://docs.python.org/3/reference/compound_stmts.html#try try / except] where we expect that a Exception could be raised: &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    x = 10 * (1 / 0)&lt;br /&gt;
except ZeroDivisionError:&lt;br /&gt;
    x = 0&lt;br /&gt;
&lt;br /&gt;
print(x) #-&amp;gt; 0&amp;lt;/syntaxhighlight&amp;gt;We can also prepare for more than one Exception:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    x = &amp;quot;1&amp;quot; + 1&lt;br /&gt;
except ZeroDivisionError:&lt;br /&gt;
    x = 0&lt;br /&gt;
except NameError:&lt;br /&gt;
    x = 1&lt;br /&gt;
except TypeError:&lt;br /&gt;
    x = 3&lt;br /&gt;
&lt;br /&gt;
print(x) # -&amp;gt; 3&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== BAD: Catch all ==&lt;br /&gt;
You could &amp;#039;&amp;#039;&amp;#039;catch all&amp;#039;&amp;#039;&amp;#039; possible exceptions like this&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    x = &amp;quot;1&amp;quot; + 1&lt;br /&gt;
except: # -&amp;gt; do not use bare &amp;#039;except&amp;#039;&lt;br /&gt;
    x = 42&lt;br /&gt;
print(x)&amp;lt;/syntaxhighlight&amp;gt;but (as you already see of the error by the linting system) &amp;#039;&amp;#039;&amp;#039;it is a very bad idea&amp;#039;&amp;#039;&amp;#039;. Everybody will think that you are not really civilized and will avoid you for the rest of your life. &lt;br /&gt;
&lt;br /&gt;
You will block exception like KeyboardInterrupt which are required to abort your program with the keyboard.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The maximum of laziness allowed is to use Exception&amp;#039;&amp;#039;&amp;#039; like show in this example: &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    x = &amp;quot;1&amp;quot; + 1&lt;br /&gt;
except Exception:&lt;br /&gt;
    x = 42&lt;br /&gt;
print(x)&amp;lt;/syntaxhighlight&amp;gt;Please take a look at the Exception hierarchy at the end of this guide. &lt;br /&gt;
&lt;br /&gt;
== Sort your exceptions ==&lt;br /&gt;
You need to sort the exceptions according the exception hierarchy. The more specialized come first. The broader ones come later. &lt;br /&gt;
&lt;br /&gt;
Correct:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    x = &amp;quot;1&amp;quot; + 1&lt;br /&gt;
except TypeError:&lt;br /&gt;
    x = 21&lt;br /&gt;
except Exception:&lt;br /&gt;
    x = 42&lt;br /&gt;
print(x) # -&amp;gt; 21&amp;lt;/syntaxhighlight&amp;gt;Wrong:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    x = &amp;quot;1&amp;quot; + 1&lt;br /&gt;
except Exception:&lt;br /&gt;
    x = 42&lt;br /&gt;
except TypeError:&lt;br /&gt;
    x = 21&lt;br /&gt;
print(x) # -&amp;gt; 42&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using the information from within the Exception ==&lt;br /&gt;
Already the build-in exceptions contain additional information:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    x = &amp;quot;1&amp;quot; + 1&lt;br /&gt;
except TypeError as e:&lt;br /&gt;
    print(type(e))&lt;br /&gt;
    print(&amp;quot;&amp;quot;)&lt;br /&gt;
    print(dir(e))&lt;br /&gt;
    print(&amp;quot;&amp;quot;)&lt;br /&gt;
    print(e)&lt;br /&gt;
    print(&amp;quot;&amp;quot;)&lt;br /&gt;
    print(e.args)&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&amp;lt;class &amp;#039;TypeError&amp;#039;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[&amp;#039;__cause__&amp;#039;, &amp;#039;__class__&amp;#039;, &amp;#039;__context__&amp;#039;, &amp;#039;__delattr__&amp;#039;, &amp;#039;__dict__&amp;#039;, &amp;#039;__dir__&amp;#039;, &amp;#039;__doc__&amp;#039;, &amp;#039;__eq__&amp;#039;, &amp;#039;__format__&amp;#039;, &amp;#039;__ge__&amp;#039;, &amp;#039;__getattribute__&amp;#039;, &amp;#039;__gt__&amp;#039;, &amp;#039;__hash__&amp;#039;, &amp;#039;__init__&amp;#039;, &amp;#039;__init_subclass__&amp;#039;, &amp;#039;__le__&amp;#039;, &amp;#039;__lt__&amp;#039;, &amp;#039;__ne__&amp;#039;, &amp;#039;__new__&amp;#039;, &amp;#039;__reduce__&amp;#039;, &amp;#039;__reduce_ex__&amp;#039;, &amp;#039;__repr__&amp;#039;, &amp;#039;__setattr__&amp;#039;, &amp;#039;__setstate__&amp;#039;, &amp;#039;__sizeof__&amp;#039;, &amp;#039;__str__&amp;#039;, &amp;#039;__subclasshook__&amp;#039;, &amp;#039;__suppress_context__&amp;#039;, &amp;#039;__traceback__&amp;#039;, &amp;#039;args&amp;#039;, &amp;#039;with_traceback&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
can only concatenate str (not &amp;quot;int&amp;quot;) to str&lt;br /&gt;
&lt;br /&gt;
(&amp;#039;can only concatenate str (not &amp;quot;int&amp;quot;) to str&amp;#039;,)&amp;lt;/syntaxhighlight&amp;gt;If we raise an exception then we can use this additional information to transfer information about the error and how to fix it. &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    raise Exception(&amp;quot;User-Error1&amp;quot;, &amp;quot;X is not a U&amp;quot;)&lt;br /&gt;
except Exception as e:&lt;br /&gt;
    print(type(e))&lt;br /&gt;
    print(&amp;quot;&amp;quot;)&lt;br /&gt;
    print(e)&lt;br /&gt;
    print(&amp;quot;&amp;quot;)&lt;br /&gt;
    print(e.args[0])&lt;br /&gt;
    print(&amp;quot;&amp;quot;)&lt;br /&gt;
    print(e.args[1])&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&amp;lt;class &amp;#039;Exception&amp;#039;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(&amp;#039;User-Error1&amp;#039;, &amp;#039;A X is not a U&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
User-Error1&lt;br /&gt;
&lt;br /&gt;
A X is not a U&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Custom Exceptions ==&lt;br /&gt;
We can and should build our own exceptions. It is recommended to create your own branch on the Exception hierarchy tree. In the example I made a branch BaseError derived from Exception. Now I can generate specialized errors from BaseError. The idea behind is that I now can catch all MY own errors with except BaseError if I don’t want to distinguish between my errors.  &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;class BaseError(Exception):&lt;br /&gt;
    def __init__(self, *args: object):&lt;br /&gt;
        super().__init__(*args)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class E1Error(BaseError):&lt;br /&gt;
    message: str&lt;br /&gt;
    error_value: str&lt;br /&gt;
&lt;br /&gt;
    def __init__(self, *args: object):&lt;br /&gt;
        super().__init__(*args)&lt;br /&gt;
        self.message = str(args[0])&lt;br /&gt;
        self.error_value = str(args[1])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
try:&lt;br /&gt;
    raise E1Error(&amp;quot;User-Error1&amp;quot;, &amp;quot;X is not a U&amp;quot;)&lt;br /&gt;
except E1Error as e:&lt;br /&gt;
    print(e.message)&lt;br /&gt;
    print(&amp;quot;&amp;quot;)&lt;br /&gt;
    print(e.error_value)&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;User-Error1&lt;br /&gt;
&lt;br /&gt;
X is not a U&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Raise Exceptions ==&lt;br /&gt;
I you want to raise an exception in your source code, then you need to determine which exception class is the best for the problem. And then raise that exception with &amp;#039;&amp;#039;&amp;#039;raise&amp;#039;&amp;#039;&amp;#039;.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;raise NameError(&amp;quot;My name is NameError&amp;quot;) # -&amp;gt; NameError: My name is NameError&amp;lt;/syntaxhighlight&amp;gt;You can also re-raise an exception if you couldn’t handle it or want to use the info for down the lane:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    try:&lt;br /&gt;
        raise NameError(&amp;quot;My name is NameError&amp;quot;)  # -&amp;gt; NameError: My name is NameError&lt;br /&gt;
    except NameError as e:&lt;br /&gt;
        print(&amp;quot;A NameError:&amp;quot;)&lt;br /&gt;
        print(e)&lt;br /&gt;
        raise&lt;br /&gt;
except Exception as e:&lt;br /&gt;
    print(&amp;quot;An Exception:&amp;quot;)&lt;br /&gt;
    print(e)&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;A NameError:&lt;br /&gt;
My name is NameError&lt;br /&gt;
An Exception:&lt;br /&gt;
My name is NameError&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The full syntax [https://docs.python.org/3/reference/compound_stmts.html#try try / except / else / finally] ==&lt;br /&gt;
We full syntax looks like this:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;try:&lt;br /&gt;
    ...&lt;br /&gt;
except Exception:&lt;br /&gt;
    ...&lt;br /&gt;
else:&lt;br /&gt;
    ...&lt;br /&gt;
finally:&lt;br /&gt;
    ...&amp;lt;/syntaxhighlight&amp;gt;&amp;#039;&amp;#039;&amp;#039;finally&amp;#039;&amp;#039;&amp;#039; is always executed. &amp;#039;&amp;#039;&amp;#039;else&amp;#039;&amp;#039;&amp;#039; is executed when NO exception was raised at all.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If there is an exception, which is not excepted by except (because the exception type was not broad enough), it will stop the program AFTER finally.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Chaining exceptions ==&lt;br /&gt;
{: .topic-optional} This is an optional topic!&lt;br /&gt;
&lt;br /&gt;
Instead of just re-raising an exception we can also add to it with &amp;#039;&amp;#039;&amp;#039;from&amp;#039;&amp;#039;&amp;#039;. &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def function() -&amp;gt; None:&lt;br /&gt;
    raise Exception(&amp;quot;I broke it!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
try:&lt;br /&gt;
    function()&lt;br /&gt;
except Exception as e:&lt;br /&gt;
    raise Exception(&amp;quot;I don&amp;#039;t care&amp;quot;) from e&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;---------------------------------------------------------------------------&lt;br /&gt;
Exception                                 Traceback (most recent call last)&lt;br /&gt;
/data_1/davrot/test/xxxx.py in line 7&lt;br /&gt;
      9 try:&lt;br /&gt;
----&amp;gt; 10     function()&lt;br /&gt;
      11 except Exception as e:&lt;br /&gt;
&lt;br /&gt;
/data_1/davrot/test/xxxx.py in line 3, in function()&lt;br /&gt;
      5 def function() -&amp;gt; None:&lt;br /&gt;
----&amp;gt; 6     raise Exception(&amp;quot;I broke it!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Exception: I broke it!&lt;br /&gt;
&lt;br /&gt;
The above exception was the direct cause of the following exception:&lt;br /&gt;
&lt;br /&gt;
Exception                                 Traceback (most recent call last)&lt;br /&gt;
/data_1/davrot/test/xxxx.py in line 9&lt;br /&gt;
      10     function()&lt;br /&gt;
      11 except Exception as e:&lt;br /&gt;
----&amp;gt; 12     raise Exception(&amp;quot;I don&amp;#039;t care&amp;quot;) from e&lt;br /&gt;
&lt;br /&gt;
Exception: I don&amp;#039;t care&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/library/exceptions.html#exception-hierarchy Exception hierarchy] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;BaseException&lt;br /&gt;
 ├── BaseExceptionGroup&lt;br /&gt;
 ├── GeneratorExit&lt;br /&gt;
 ├── KeyboardInterrupt&lt;br /&gt;
 ├── SystemExit&lt;br /&gt;
 └── Exception&lt;br /&gt;
      ├── ArithmeticError&lt;br /&gt;
      │    ├── FloatingPointError&lt;br /&gt;
      │    ├── OverflowError&lt;br /&gt;
      │    └── ZeroDivisionError&lt;br /&gt;
      ├── AssertionError&lt;br /&gt;
      ├── AttributeError&lt;br /&gt;
      ├── BufferError&lt;br /&gt;
      ├── EOFError&lt;br /&gt;
      ├── ExceptionGroup [BaseExceptionGroup]&lt;br /&gt;
      ├── ImportError&lt;br /&gt;
      │    └── ModuleNotFoundError&lt;br /&gt;
      ├── LookupError&lt;br /&gt;
      │    ├── IndexError&lt;br /&gt;
      │    └── KeyError&lt;br /&gt;
      ├── MemoryError&lt;br /&gt;
      ├── NameError&lt;br /&gt;
      │    └── UnboundLocalError&lt;br /&gt;
      ├── OSError&lt;br /&gt;
      │    ├── BlockingIOError&lt;br /&gt;
      │    ├── ChildProcessError&lt;br /&gt;
      │    ├── ConnectionError&lt;br /&gt;
      │    │    ├── BrokenPipeError&lt;br /&gt;
      │    │    ├── ConnectionAbortedError&lt;br /&gt;
      │    │    ├── ConnectionRefusedError&lt;br /&gt;
      │    │    └── ConnectionResetError&lt;br /&gt;
      │    ├── FileExistsError&lt;br /&gt;
      │    ├── FileNotFoundError&lt;br /&gt;
      │    ├── InterruptedError&lt;br /&gt;
      │    ├── IsADirectoryError&lt;br /&gt;
      │    ├── NotADirectoryError&lt;br /&gt;
      │    ├── PermissionError&lt;br /&gt;
      │    ├── ProcessLookupError&lt;br /&gt;
      │    └── TimeoutError&lt;br /&gt;
      ├── ReferenceError&lt;br /&gt;
      ├── RuntimeError&lt;br /&gt;
      │    ├── NotImplementedError&lt;br /&gt;
      │    └── RecursionError&lt;br /&gt;
      ├── StopAsyncIteration&lt;br /&gt;
      ├── StopIteration&lt;br /&gt;
      ├── SyntaxError&lt;br /&gt;
      │    └── IndentationError&lt;br /&gt;
      │         └── TabError&lt;br /&gt;
      ├── SystemError&lt;br /&gt;
      ├── TypeError&lt;br /&gt;
      ├── ValueError&lt;br /&gt;
      │    └── UnicodeError&lt;br /&gt;
      │         ├── UnicodeDecodeError&lt;br /&gt;
      │         ├── UnicodeEncodeError&lt;br /&gt;
      │         └── UnicodeTranslateError&lt;br /&gt;
      └── Warning&lt;br /&gt;
           ├── BytesWarning&lt;br /&gt;
           ├── DeprecationWarning&lt;br /&gt;
           ├── EncodingWarning&lt;br /&gt;
           ├── FutureWarning&lt;br /&gt;
           ├── ImportWarning&lt;br /&gt;
           ├── PendingDeprecationWarning&lt;br /&gt;
           ├── ResourceWarning&lt;br /&gt;
           ├── RuntimeWarning&lt;br /&gt;
           ├── SyntaxWarning&lt;br /&gt;
           ├── UnicodeWarning&lt;br /&gt;
           └── UserWarning&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Resources ==&lt;br /&gt;
&lt;br /&gt;
* [https://docs.python.org/3/library/exceptions.html Built-in Exceptions]&lt;/div&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
</feed>