/
usr
/
share
/
doc
/
python3-docs
/
html
/
library
/
/usr/share/doc/python3-docs/html/library
mkdir
upload
Name
Size
Mode
Actions
2to3.html
61709
0644
edit
dl
rm
abc.html
41394
0644
edit
dl
rm
aifc.html
28114
0644
edit
dl
rm
allos.html
32236
0644
edit
dl
rm
archiving.html
11992
0644
edit
dl
rm
argparse.html
274984
0644
edit
dl
rm
array.html
33547
0644
edit
dl
rm
ast.html
39595
0644
edit
dl
rm
asynchat.html
32546
0644
edit
dl
rm
asyncio-dev.html
51869
0644
edit
dl
rm
asyncio-eventloop.html
111215
0644
edit
dl
rm
asyncio-eventloops.html
33026
0644
edit
dl
rm
asyncio-protocol.html
87431
0644
edit
dl
rm
asyncio-queue.html
23957
0644
edit
dl
rm
asyncio-stream.html
61005
0644
edit
dl
rm
asyncio-subprocess.html
58015
0644
edit
dl
rm
asyncio-sync.html
35678
0644
edit
dl
rm
asyncio-task.html
83968
0644
edit
dl
rm
asyncio.html
28026
0644
edit
dl
rm
asyncore.html
42706
0644
edit
dl
rm
atexit.html
18433
0644
edit
dl
rm
audioop.html
35442
0644
edit
dl
rm
base64.html
36454
0644
edit
dl
rm
bdb.html
43423
0644
edit
dl
rm
binary.html
12723
0644
edit
dl
rm
binascii.html
24745
0644
edit
dl
rm
binhex.html
11669
0644
edit
dl
rm
bisect.html
25602
0644
edit
dl
rm
builtins.html
11604
0644
edit
dl
rm
bz2.html
33358
0644
edit
dl
rm
calendar.html
39539
0644
edit
dl
rm
cgi.html
55881
0644
edit
dl
rm
cgitb.html
12765
0644
edit
dl
rm
chunk.html
16872
0644
edit
dl
rm
cmath.html
34171
0644
edit
dl
rm
cmd.html
43811
0644
edit
dl
rm
code.html
27886
0644
edit
dl
rm
codecs.html
134071
0644
edit
dl
rm
codeop.html
14304
0644
edit
dl
rm
collections.abc.html
60912
0644
edit
dl
rm
collections.html
154946
0644
edit
dl
rm
colorsys.html
12627
0644
edit
dl
rm
compileall.html
28263
0644
edit
dl
rm
concurrency.html
21203
0644
edit
dl
rm
concurrent.futures.html
60660
0644
edit
dl
rm
concurrent.html
7959
0644
edit
dl
rm
configparser.html
141052
0644
edit
dl
rm
constants.html
17500
0644
edit
dl
rm
contextlib.html
88801
0644
edit
dl
rm
copy.html
13841
0644
edit
dl
rm
copyreg.html
14725
0644
edit
dl
rm
crypt.html
20331
0644
edit
dl
rm
crypto.html
10914
0644
edit
dl
rm
csv.html
69043
0644
edit
dl
rm
ctypes.html
264529
0644
edit
dl
rm
curses.ascii.html
25197
0644
edit
dl
rm
curses.html
175551
0644
edit
dl
rm
curses.panel.html
16706
0644
edit
dl
rm
custominterp.html
8734
0644
edit
dl
rm
datatypes.html
23950
0644
edit
dl
rm
datetime.html
306435
0644
edit
dl
rm
dbm.html
43119
0644
edit
dl
rm
debug.html
13983
0644
edit
dl
rm
decimal.html
233486
0644
edit
dl
rm
development.html
25123
0644
edit
dl
rm
difflib.html
96156
0644
edit
dl
rm
dis.html
106191
0644
edit
dl
rm
distribution.html
10506
0644
edit
dl
rm
distutils.html
10884
0644
edit
dl
rm
doctest.html
179752
0644
edit
dl
rm
dummy_threading.html
9316
0644
edit
dl
rm
email.charset.html
26431
0644
edit
dl
rm
email.compat32-message.html
86150
0644
edit
dl
rm
email.contentmanager.html
30028
0644
edit
dl
rm
email.encoders.html
14250
0644
edit
dl
rm
email.errors.html
19704
0644
edit
dl
rm
email.examples.html
57872
0644
edit
dl
rm
email.generator.html
41798
0644
edit
dl
rm
email.header.html
31219
0644
edit
dl
rm
email.headerregistry.html
57670
0644
edit
dl
rm
email.html
27741
0644
edit
dl
rm
email.iterators.html
13532
0644
edit
dl
rm
email.message.html
87210
0644
edit
dl
rm
email.mime.html
36081
0644
edit
dl
rm
email.parser.html
47430
0644
edit
dl
rm
email.policy.html
75241
0644
edit
dl
rm
email.util.html
33853
0644
edit
dl
rm
ensurepip.html
18700
0644
edit
dl
rm
enum.html
133949
0644
edit
dl
rm
errno.html
41331
0644
edit
dl
rm
exceptions.html
85151
0644
edit
dl
rm
faulthandler.html
23396
0644
edit
dl
rm
fcntl.html
27321
0644
edit
dl
rm
filecmp.html
25036
0644
edit
dl
rm
fileformats.html
11129
0644
edit
dl
rm
fileinput.html
30030
0644
edit
dl
rm
filesys.html
13418
0644
edit
dl
rm
fnmatch.html
16987
0644
edit
dl
rm
formatter.html
37666
0644
edit
dl
rm
fpectl.html
17538
0644
edit
dl
rm
fractions.html
29918
0644
edit
dl
rm
frameworks.html
13471
0644
edit
dl
rm
ftplib.html
54429
0644
edit
dl
rm
functional.html
9002
0644
edit
dl
rm
functions.html
201486
0644
edit
dl
rm
functools.html
64951
0644
edit
dl
rm
gc.html
29524
0644
edit
dl
rm
getopt.html
25353
0644
edit
dl
rm
getpass.html
12061
0644
edit
dl
rm
gettext.html
76641
0644
edit
dl
rm
glob.html
18432
0644
edit
dl
rm
grp.html
12383
0644
edit
dl
rm
gzip.html
32669
0644
edit
dl
rm
hashlib.html
79554
0644
edit
dl
rm
heapq.html
35529
0644
edit
dl
rm
hmac.html
17827
0644
edit
dl
rm
html.entities.html
11361
0644
edit
dl
rm
html.html
11108
0644
edit
dl
rm
html.parser.html
41842
0644
edit
dl
rm
http.client.html
65250
0644
edit
dl
rm
http.cookiejar.html
96195
0644
edit
dl
rm
http.cookies.html
39329
0644
edit
dl
rm
http.html
37602
0644
edit
dl
rm
http.server.html
58191
0644
edit
dl
rm
i18n.html
10464
0644
edit
dl
rm
idle.html
48412
0644
edit
dl
rm
imaplib.html
67339
0644
edit
dl
rm
imghdr.html
13456
0644
edit
dl
rm
imp.html
48153
0644
edit
dl
rm
importlib.html
173109
0644
edit
dl
rm
index.html
67768
0644
edit
dl
rm
inspect.html
130355
0644
edit
dl
rm
internet.html
31087
0644
edit
dl
rm
intro.html
9904
0644
edit
dl
rm
io.html
122685
0644
edit
dl
rm
ipaddress.html
105873
0644
edit
dl
rm
ipc.html
33743
0644
edit
dl
rm
itertools.html
132824
0644
edit
dl
rm
json.html
81078
0644
edit
dl
rm
keyword.html
8918
0644
edit
dl
rm
language.html
13648
0644
edit
dl
rm
linecache.html
13492
0644
edit
dl
rm
locale.html
61540
0644
edit
dl
rm
logging.config.html
85489
0644
edit
dl
rm
logging.handlers.html
112397
0644
edit
dl
rm
logging.html
134850
0644
edit
dl
rm
lzma.html
56484
0644
edit
dl
rm
macpath.html
9257
0644
edit
dl
rm
mailbox.html
157315
0644
edit
dl
rm
mailcap.html
14676
0644
edit
dl
rm
markup.html
19356
0644
edit
dl
rm
marshal.html
18317
0644
edit
dl
rm
math.html
53661
0644
edit
dl
rm
mimetypes.html
31410
0644
edit
dl
rm
misc.html
8123
0644
edit
dl
rm
mm.html
10022
0644
edit
dl
rm
mmap.html
34294
0644
edit
dl
rm
modulefinder.html
18559
0644
edit
dl
rm
modules.html
10914
0644
edit
dl
rm
msilib.html
57205
0644
edit
dl
rm
msvcrt.html
21989
0644
edit
dl
rm
multiprocessing.html
365487
0644
edit
dl
rm
netdata.html
17470
0644
edit
dl
rm
netrc.html
14568
0644
edit
dl
rm
nis.html
11659
0644
edit
dl
rm
nntplib.html
69601
0644
edit
dl
rm
numbers.html
41432
0644
edit
dl
rm
numeric.html
15535
0644
edit
dl
rm
operator.html
81460
0644
edit
dl
rm
optparse.html
249942
0644
edit
dl
rm
os.html
398039
0644
edit
dl
rm
os.path.html
53818
0644
edit
dl
rm
ossaudiodev.html
47947
0644
edit
dl
rm
othergui.html
10256
0644
edit
dl
rm
parser.html
42408
0644
edit
dl
rm
pathlib.html
116726
0644
edit
dl
rm
pdb.html
52661
0644
edit
dl
rm
persistence.html
15281
0644
edit
dl
rm
pickle.html
115052
0644
edit
dl
rm
pickletools.html
17094
0644
edit
dl
rm
pipes.html
15991
0644
edit
dl
rm
pkgutil.html
31878
0644
edit
dl
rm
platform.html
33728
0644
edit
dl
rm
plistlib.html
29290
0644
edit
dl
rm
poplib.html
30940
0644
edit
dl
rm
posix.html
17608
0644
edit
dl
rm
pprint.html
41969
0644
edit
dl
rm
profile.html
71848
0644
edit
dl
rm
pty.html
17594
0644
edit
dl
rm
pwd.html
13098
0644
edit
dl
rm
pyclbr.html
16520
0644
edit
dl
rm
pydoc.html
15991
0644
edit
dl
rm
pyexpat.html
88938
0644
edit
dl
rm
python.html
17133
0644
edit
dl
rm
py_compile.html
15508
0644
edit
dl
rm
queue.html
28199
0644
edit
dl
rm
quopri.html
13858
0644
edit
dl
rm
random.html
60646
0644
edit
dl
rm
re.html
191176
0644
edit
dl
rm
readline.html
42133
0644
edit
dl
rm
reprlib.html
24964
0644
edit
dl
rm
resource.html
36552
0644
edit
dl
rm
rlcompleter.html
14392
0644
edit
dl
rm
runpy.html
26607
0644
edit
dl
rm
sched.html
20034
0644
edit
dl
rm
secrets.html
24734
0644
edit
dl
rm
select.html
62819
0644
edit
dl
rm
selectors.html
35845
0644
edit
dl
rm
shelve.html
28777
0644
edit
dl
rm
shlex.html
48979
0644
edit
dl
rm
shutil.html
74207
0644
edit
dl
rm
signal.html
55064
0644
edit
dl
rm
site.html
32242
0644
edit
dl
rm
smtpd.html
35892
0644
edit
dl
rm
smtplib.html
68433
0644
edit
dl
rm
sndhdr.html
12439
0644
edit
dl
rm
socket.html
196077
0644
edit
dl
rm
socketserver.html
81918
0644
edit
dl
rm
spwd.html
12328
0644
edit
dl
rm
sqlite3.html
161872
0644
edit
dl
rm
ssl.html
266327
0644
edit
dl
rm
stat.html
44722
0644
edit
dl
rm
statistics.html
50050
0644
edit
dl
rm
stdtypes.html
465658
0644
edit
dl
rm
string.html
95262
0644
edit
dl
rm
stringprep.html
18654
0644
edit
dl
rm
struct.html
51100
0644
edit
dl
rm
subprocess.html
141926
0644
edit
dl
rm
sunau.html
31757
0644
edit
dl
rm
superseded.html
13781
0644
edit
dl
rm
symbol.html
8848
0644
edit
dl
rm
symtable.html
26112
0644
edit
dl
rm
sys.html
141670
0644
edit
dl
rm
sysconfig.html
30259
0644
edit
dl
rm
syslog.html
21026
0644
edit
dl
rm
tabnanny.html
12269
0644
edit
dl
rm
tarfile.html
96758
0644
edit
dl
rm
telnetlib.html
30409
0644
edit
dl
rm
tempfile.html
41394
0644
edit
dl
rm
termios.html
18181
0644
edit
dl
rm
test.html
78555
0644
edit
dl
rm
text.html
13786
0644
edit
dl
rm
textwrap.html
37998
0644
edit
dl
rm
threading.html
103464
0644
edit
dl
rm
time.html
79629
0644
edit
dl
rm
timeit.html
45249
0644
edit
dl
rm
tk.html
23299
0644
edit
dl
rm
tkinter.html
82932
0644
edit
dl
rm
tkinter.scrolledtext.html
9957
0644
edit
dl
rm
tkinter.tix.html
51858
0644
edit
dl
rm
tkinter.ttk.html
110673
0644
edit
dl
rm
token.html
22660
0644
edit
dl
rm
tokenize.html
32149
0644
edit
dl
rm
trace.html
29145
0644
edit
dl
rm
traceback.html
57588
0644
edit
dl
rm
tracemalloc.html
101144
0644
edit
dl
rm
tty.html
10540
0644
edit
dl
rm
turtle.html
250367
0644
edit
dl
rm
types.html
37060
0644
edit
dl
rm
typing.html
139175
0644
edit
dl
rm
undoc.html
9181
0644
edit
dl
rm
unicodedata.html
21108
0644
edit
dl
rm
unittest.html
250689
0644
edit
dl
rm
unittest.mock-examples.html
168144
0644
edit
dl
rm
unittest.mock.html
300089
0644
edit
dl
rm
unix.html
11780
0644
edit
dl
rm
urllib.error.html
14474
0644
edit
dl
rm
urllib.html
9566
0644
edit
dl
rm
urllib.parse.html
76334
0644
edit
dl
rm
urllib.request.html
177199
0644
edit
dl
rm
urllib.robotparser.html
17156
0644
edit
dl
rm
uu.html
12209
0644
edit
dl
rm
uuid.html
31957
0644
edit
dl
rm
venv.html
73909
0644
edit
dl
rm
warnings.html
50320
0644
edit
dl
rm
wave.html
29868
0644
edit
dl
rm
weakref.html
65292
0644
edit
dl
rm
webbrowser.html
26901
0644
edit
dl
rm
windows.html
10626
0644
edit
dl
rm
winreg.html
66730
0644
edit
dl
rm
winsound.html
21268
0644
edit
dl
rm
wsgiref.html
94492
0644
edit
dl
rm
xdrlib.html
33751
0644
edit
dl
rm
xml.dom.html
97570
0644
edit
dl
rm
xml.dom.minidom.html
41481
0644
edit
dl
rm
xml.dom.pulldom.html
24214
0644
edit
dl
rm
xml.etree.elementtree.html
135491
0644
edit
dl
rm
xml.html
19261
0644
edit
dl
rm
xml.sax.handler.html
42659
0644
edit
dl
rm
xml.sax.html
25515
0644
edit
dl
rm
xml.sax.reader.html
45869
0644
edit
dl
rm
xml.sax.utils.html
16928
0644
edit
dl
rm
xmlrpc.client.html
71947
0644
edit
dl
rm
xmlrpc.html
8535
0644
edit
dl
rm
xmlrpc.server.html
49195
0644
edit
dl
rm
zipapp.html
45105
0644
edit
dl
rm
zipfile.html
80039
0644
edit
dl
rm
zipimport.html
23052
0644
edit
dl
rm
zlib.html
34557
0644
edit
dl
rm
_dummy_thread.html
9193
0644
edit
dl
rm
_thread.html
22588
0644
edit
dl
rm
__future__.html
15792
0644
edit
dl
rm
__main__.html
9019
0644
edit
dl
rm
Edit:
/usr/share/doc/python3-docs/html/library/exceptions.html
(85151B)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>5. Built-in Exceptions — Python 3.6.7 documentation</title> <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 3.6.7 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="6. Text Processing Services" href="text.html" /> <link rel="prev" title="4. Built-in Types" href="stdtypes.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="https://docs.python.org/3/library/exceptions.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="text.html" title="6. Text Processing Services" accesskey="N">next</a> |</li> <li class="right" > <a href="stdtypes.html" title="4. Built-in Types" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">3.6.7 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Standard Library</a> »</li> <li class="right"> <div class="inline-search" style="display: none" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('.inline-search').show(0);</script> | </li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="built-in-exceptions"> <span id="bltin-exceptions"></span><h1>5. Built-in Exceptions<a class="headerlink" href="#built-in-exceptions" title="Permalink to this headline">¶</a></h1> <p id="index-0">In Python, all exceptions must be instances of a class that derives from <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseException</span></code></a>. In a <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> statement with an <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which <em>it</em> is derived). Two exception classes that are not related via subclassing are never equivalent, even if they have the same name.</p> <p id="index-1">The built-in exceptions listed below can be generated by the interpreter or built-in functions. Except where mentioned, they have an “associated value” indicating the detailed cause of the error. This may be a string or a tuple of several items of information (e.g., an error code and a string explaining the code). The associated value is usually passed as arguments to the exception class’s constructor.</p> <p>User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition “just like” the situation in which the interpreter raises the same exception; but beware that there is nothing to prevent user code from raising an inappropriate error.</p> <p>The built-in exception classes can be subclassed to define new exceptions; programmers are encouraged to derive new exceptions from the <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> class or one of its subclasses, and not from <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a>. More information on defining exceptions is available in the Python Tutorial under <a class="reference internal" href="../tutorial/errors.html#tut-userexceptions"><span class="std std-ref">User-defined Exceptions</span></a>.</p> <p>When raising (or re-raising) an exception in an <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> or <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> clause <code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code> is automatically set to the last exception caught; if the new exception is not handled the traceback that is eventually displayed will include the originating exception(s) and the final exception.</p> <p>When raising a new exception (rather than using a bare <code class="docutils literal notranslate"><span class="pre">raise</span></code> to re-raise the exception currently being handled), the implicit exception context can be supplemented with an explicit cause by using <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> with <a class="reference internal" href="../reference/simple_stmts.html#raise"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a>:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">raise</span> <span class="n">new_exc</span> <span class="kn">from</span> <span class="nn">original_exc</span> </pre></div> </div> <p>The expression following <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> must be an exception or <code class="docutils literal notranslate"><span class="pre">None</span></code>. It will be set as <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> on the raised exception. Setting <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> also implicitly sets the <code class="xref py py-attr docutils literal notranslate"><span class="pre">__suppress_context__</span></code> attribute to <code class="docutils literal notranslate"><span class="pre">True</span></code>, so that using <code class="docutils literal notranslate"><span class="pre">raise</span> <span class="pre">new_exc</span> <span class="pre">from</span> <span class="pre">None</span></code> effectively replaces the old exception with the new one for display purposes (e.g. converting <a class="reference internal" href="#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> to <a class="reference internal" href="#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>, while leaving the old exception available in <code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code> for introspection when debugging.</p> <p>The default traceback display code shows these chained exceptions in addition to the traceback for the exception itself. An explicitly chained exception in <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> is always shown when present. An implicitly chained exception in <code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code> is shown only if <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> is <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__suppress_context__</span></code> is false.</p> <p>In either case, the exception itself is always shown after any chained exceptions so that the final line of the traceback always shows the last exception that was raised.</p> <div class="section" id="base-classes"> <h2>5.1. Base classes<a class="headerlink" href="#base-classes" title="Permalink to this headline">¶</a></h2> <p>The following exceptions are used mostly as base classes for other exceptions.</p> <dl class="exception"> <dt id="BaseException"> <em class="property">exception </em><code class="descname">BaseException</code><a class="headerlink" href="#BaseException" title="Permalink to this definition">¶</a></dt> <dd><p>The base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes (for that, use <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>). If <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> is called on an instance of this class, the representation of the argument(s) to the instance are returned, or the empty string when there were no arguments.</p> <dl class="attribute"> <dt id="BaseException.args"> <code class="descname">args</code><a class="headerlink" href="#BaseException.args" title="Permalink to this definition">¶</a></dt> <dd><p>The tuple of arguments given to the exception constructor. Some built-in exceptions (like <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>) expect a certain number of arguments and assign a special meaning to the elements of this tuple, while others are usually called only with a single string giving an error message.</p> </dd></dl> <dl class="method"> <dt id="BaseException.with_traceback"> <code class="descname">with_traceback</code><span class="sig-paren">(</span><em>tb</em><span class="sig-paren">)</span><a class="headerlink" href="#BaseException.with_traceback" title="Permalink to this definition">¶</a></dt> <dd><p>This method sets <em>tb</em> as the new traceback for the exception and returns the exception object. It is usually used in exception handling code like this:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span> <span class="o">...</span> <span class="k">except</span> <span class="n">SomeException</span><span class="p">:</span> <span class="n">tb</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">2</span><span class="p">]</span> <span class="k">raise</span> <span class="n">OtherException</span><span class="p">(</span><span class="o">...</span><span class="p">)</span><span class="o">.</span><span class="n">with_traceback</span><span class="p">(</span><span class="n">tb</span><span class="p">)</span> </pre></div> </div> </dd></dl> </dd></dl> <dl class="exception"> <dt id="Exception"> <em class="property">exception </em><code class="descname">Exception</code><a class="headerlink" href="#Exception" title="Permalink to this definition">¶</a></dt> <dd><p>All built-in, non-system-exiting exceptions are derived from this class. All user-defined exceptions should also be derived from this class.</p> </dd></dl> <dl class="exception"> <dt id="ArithmeticError"> <em class="property">exception </em><code class="descname">ArithmeticError</code><a class="headerlink" href="#ArithmeticError" title="Permalink to this definition">¶</a></dt> <dd><p>The base class for those built-in exceptions that are raised for various arithmetic errors: <a class="reference internal" href="#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>, <a class="reference internal" href="#ZeroDivisionError" title="ZeroDivisionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a>, <a class="reference internal" href="#FloatingPointError" title="FloatingPointError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FloatingPointError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="BufferError"> <em class="property">exception </em><code class="descname">BufferError</code><a class="headerlink" href="#BufferError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">buffer</span></a> related operation cannot be performed.</p> </dd></dl> <dl class="exception"> <dt id="LookupError"> <em class="property">exception </em><code class="descname">LookupError</code><a class="headerlink" href="#LookupError" title="Permalink to this definition">¶</a></dt> <dd><p>The base class for the exceptions that are raised when a key or index used on a mapping or sequence is invalid: <a class="reference internal" href="#IndexError" title="IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a>, <a class="reference internal" href="#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>. This can be raised directly by <a class="reference internal" href="codecs.html#codecs.lookup" title="codecs.lookup"><code class="xref py py-func docutils literal notranslate"><span class="pre">codecs.lookup()</span></code></a>.</p> </dd></dl> </div> <div class="section" id="concrete-exceptions"> <h2>5.2. Concrete exceptions<a class="headerlink" href="#concrete-exceptions" title="Permalink to this headline">¶</a></h2> <p>The following exceptions are the exceptions that are usually raised.</p> <dl class="exception"> <dt id="AssertionError"> <em class="property">exception </em><code class="descname">AssertionError</code><a class="headerlink" href="#AssertionError" title="Permalink to this definition">¶</a></dt> <dd><p id="index-2">Raised when an <a class="reference internal" href="../reference/simple_stmts.html#assert"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">assert</span></code></a> statement fails.</p> </dd></dl> <dl class="exception"> <dt id="AttributeError"> <em class="property">exception </em><code class="descname">AttributeError</code><a class="headerlink" href="#AttributeError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when an attribute reference (see <a class="reference internal" href="../reference/expressions.html#attribute-references"><span class="std std-ref">Attribute references</span></a>) or assignment fails. (When an object does not support attribute references or attribute assignments at all, <a class="reference internal" href="#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.)</p> </dd></dl> <dl class="exception"> <dt id="EOFError"> <em class="property">exception </em><code class="descname">EOFError</code><a class="headerlink" href="#EOFError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when the <a class="reference internal" href="functions.html#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a> function hits an end-of-file condition (EOF) without reading any data. (N.B.: the <code class="xref py py-meth docutils literal notranslate"><span class="pre">io.IOBase.read()</span></code> and <a class="reference internal" href="io.html#io.IOBase.readline" title="io.IOBase.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">io.IOBase.readline()</span></code></a> methods return an empty string when they hit EOF.)</p> </dd></dl> <dl class="exception"> <dt id="FloatingPointError"> <em class="property">exception </em><code class="descname">FloatingPointError</code><a class="headerlink" href="#FloatingPointError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a floating point operation fails. This exception is always defined, but can only be raised when Python is configured with the <code class="docutils literal notranslate"><span class="pre">--with-fpectl</span></code> option, or the <code class="xref py py-const docutils literal notranslate"><span class="pre">WANT_SIGFPE_HANDLER</span></code> symbol is defined in the <code class="file docutils literal notranslate"><span class="pre">pyconfig.h</span></code> file.</p> </dd></dl> <dl class="exception"> <dt id="GeneratorExit"> <em class="property">exception </em><code class="descname">GeneratorExit</code><a class="headerlink" href="#GeneratorExit" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> or <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> is closed; see <a class="reference internal" href="../reference/expressions.html#generator.close" title="generator.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">generator.close()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#coroutine.close" title="coroutine.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">coroutine.close()</span></code></a>. It directly inherits from <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> instead of <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> since it is technically not an error.</p> </dd></dl> <dl class="exception"> <dt id="ImportError"> <em class="property">exception </em><code class="descname">ImportError</code><a class="headerlink" href="#ImportError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement has troubles trying to load a module. Also raised when the “from list” in <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">...</span> <span class="pre">import</span></code> has a name that cannot be found.</p> <p>The <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code> attributes can be set using keyword-only arguments to the constructor. When set they represent the name of the module that was attempted to be imported and the path to any file which triggered the exception, respectively.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.3: </span>Added the <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code> attributes.</p> </div> </dd></dl> <dl class="exception"> <dt id="ModuleNotFoundError"> <em class="property">exception </em><code class="descname">ModuleNotFoundError</code><a class="headerlink" href="#ModuleNotFoundError" title="Permalink to this definition">¶</a></dt> <dd><p>A subclass of <a class="reference internal" href="#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> which is raised by <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> when a module could not be located. It is also raised when <code class="docutils literal notranslate"><span class="pre">None</span></code> is found in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.6.</span></p> </div> </dd></dl> <dl class="exception"> <dt id="IndexError"> <em class="property">exception </em><code class="descname">IndexError</code><a class="headerlink" href="#IndexError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a sequence subscript is out of range. (Slice indices are silently truncated to fall in the allowed range; if an index is not an integer, <a class="reference internal" href="#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.)</p> </dd></dl> <dl class="exception"> <dt id="KeyError"> <em class="property">exception </em><code class="descname">KeyError</code><a class="headerlink" href="#KeyError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a mapping (dictionary) key is not found in the set of existing keys.</p> </dd></dl> <dl class="exception"> <dt id="KeyboardInterrupt"> <em class="property">exception </em><code class="descname">KeyboardInterrupt</code><a class="headerlink" href="#KeyboardInterrupt" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when the user hits the interrupt key (normally <kbd class="kbd docutils literal notranslate">Control-C</kbd> or <kbd class="kbd docutils literal notranslate">Delete</kbd>). During execution, a check for interrupts is made regularly. The exception inherits from <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> so as to not be accidentally caught by code that catches <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> and thus prevent the interpreter from exiting.</p> </dd></dl> <dl class="exception"> <dt id="MemoryError"> <em class="property">exception </em><code class="descname">MemoryError</code><a class="headerlink" href="#MemoryError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when an operation runs out of memory but the situation may still be rescued (by deleting some objects). The associated value is a string indicating what kind of (internal) operation ran out of memory. Note that because of the underlying memory management architecture (C’s <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> function), the interpreter may not always be able to completely recover from this situation; it nevertheless raises an exception so that a stack traceback can be printed, in case a run-away program was the cause.</p> </dd></dl> <dl class="exception"> <dt id="NameError"> <em class="property">exception </em><code class="descname">NameError</code><a class="headerlink" href="#NameError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found.</p> </dd></dl> <dl class="exception"> <dt id="NotImplementedError"> <em class="property">exception </em><code class="descname">NotImplementedError</code><a class="headerlink" href="#NotImplementedError" title="Permalink to this definition">¶</a></dt> <dd><p>This exception is derived from <a class="reference internal" href="#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>. In user defined base classes, abstract methods should raise this exception when they require derived classes to override the method, or while the class is being developed to indicate that the real implementation still needs to be added.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">It should not be used to indicate that an operator or method is not meant to be supported at all – in that case either leave the operator / method undefined or, if a subclass, set it to <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-data docutils literal notranslate"><span class="pre">None</span></code></a>.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><code class="docutils literal notranslate"><span class="pre">NotImplementedError</span></code> and <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code> are not interchangeable, even though they have similar names and purposes. See <a class="reference internal" href="constants.html#NotImplemented" title="NotImplemented"><code class="xref py py-data docutils literal notranslate"><span class="pre">NotImplemented</span></code></a> for details on when to use it.</p> </div> </dd></dl> <dl class="exception"> <dt id="OSError"> <em class="property">exception </em><code class="descname">OSError</code><span class="sig-paren">(</span><span class="optional">[</span><em>arg</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#OSError" title="Permalink to this definition">¶</a></dt> <dt> <em class="property">exception </em><code class="descname">OSError</code><span class="sig-paren">(</span><em>errno</em>, <em>strerror</em><span class="optional">[</span>, <em>filename</em><span class="optional">[</span>, <em>winerror</em><span class="optional">[</span>, <em>filename2</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt> <dd><p id="index-3">This exception is raised when a system function returns a system-related error, including I/O failures such as “file not found” or “disk full” (not for illegal argument types or other incidental errors).</p> <p>The second form of the constructor sets the corresponding attributes, described below. The attributes default to <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if not specified. For backwards compatibility, if three arguments are passed, the <a class="reference internal" href="#BaseException.args" title="BaseException.args"><code class="xref py py-attr docutils literal notranslate"><span class="pre">args</span></code></a> attribute contains only a 2-tuple of the first two constructor arguments.</p> <p>The constructor often actually returns a subclass of <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>, as described in <a class="reference internal" href="#os-exceptions">OS exceptions</a> below. The particular subclass depends on the final <a class="reference internal" href="#OSError.errno" title="OSError.errno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errno</span></code></a> value. This behaviour only occurs when constructing <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> directly or via an alias, and is not inherited when subclassing.</p> <dl class="attribute"> <dt id="OSError.errno"> <code class="descname">errno</code><a class="headerlink" href="#OSError.errno" title="Permalink to this definition">¶</a></dt> <dd><p>A numeric error code from the C variable <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code>.</p> </dd></dl> <dl class="attribute"> <dt id="OSError.winerror"> <code class="descname">winerror</code><a class="headerlink" href="#OSError.winerror" title="Permalink to this definition">¶</a></dt> <dd><p>Under Windows, this gives you the native Windows error code. The <a class="reference internal" href="#OSError.errno" title="OSError.errno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errno</span></code></a> attribute is then an approximate translation, in POSIX terms, of that native error code.</p> <p>Under Windows, if the <em>winerror</em> constructor argument is an integer, the <a class="reference internal" href="#OSError.errno" title="OSError.errno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errno</span></code></a> attribute is determined from the Windows error code, and the <em>errno</em> argument is ignored. On other platforms, the <em>winerror</em> argument is ignored, and the <a class="reference internal" href="#OSError.winerror" title="OSError.winerror"><code class="xref py py-attr docutils literal notranslate"><span class="pre">winerror</span></code></a> attribute does not exist.</p> </dd></dl> <dl class="attribute"> <dt id="OSError.strerror"> <code class="descname">strerror</code><a class="headerlink" href="#OSError.strerror" title="Permalink to this definition">¶</a></dt> <dd><p>The corresponding error message, as provided by the operating system. It is formatted by the C functions <code class="xref c c-func docutils literal notranslate"><span class="pre">perror()</span></code> under POSIX, and <code class="xref c c-func docutils literal notranslate"><span class="pre">FormatMessage()</span></code> under Windows.</p> </dd></dl> <dl class="attribute"> <dt id="OSError.filename"> <code class="descname">filename</code><a class="headerlink" href="#OSError.filename" title="Permalink to this definition">¶</a></dt> <dt id="OSError.filename2"> <code class="descname">filename2</code><a class="headerlink" href="#OSError.filename2" title="Permalink to this definition">¶</a></dt> <dd><p>For exceptions that involve a file system path (such as <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> or <a class="reference internal" href="os.html#os.unlink" title="os.unlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.unlink()</span></code></a>), <a class="reference internal" href="#OSError.filename" title="OSError.filename"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code></a> is the file name passed to the function. For functions that involve two file system paths (such as <a class="reference internal" href="os.html#os.rename" title="os.rename"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.rename()</span></code></a>), <a class="reference internal" href="#OSError.filename2" title="OSError.filename2"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename2</span></code></a> corresponds to the second file name passed to the function.</p> </dd></dl> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.3: </span><a class="reference internal" href="#EnvironmentError" title="EnvironmentError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EnvironmentError</span></code></a>, <a class="reference internal" href="#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>, <a class="reference internal" href="#WindowsError" title="WindowsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">WindowsError</span></code></a>, <a class="reference internal" href="socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.error</span></code></a>, <a class="reference internal" href="select.html#select.error" title="select.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">select.error</span></code></a> and <code class="xref py py-exc docutils literal notranslate"><span class="pre">mmap.error</span></code> have been merged into <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>, and the constructor may return a subclass.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>The <a class="reference internal" href="#OSError.filename" title="OSError.filename"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code></a> attribute is now the original file name passed to the function, instead of the name encoded to or decoded from the filesystem encoding. Also, the <em>filename2</em> constructor argument and attribute was added.</p> </div> </dd></dl> <dl class="exception"> <dt id="OverflowError"> <em class="property">exception </em><code class="descname">OverflowError</code><a class="headerlink" href="#OverflowError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when the result of an arithmetic operation is too large to be represented. This cannot occur for integers (which would rather raise <a class="reference internal" href="#MemoryError" title="MemoryError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">MemoryError</span></code></a> than give up). However, for historical reasons, OverflowError is sometimes raised for integers that are outside a required range. Because of the lack of standardization of floating point exception handling in C, most floating point operations are not checked.</p> </dd></dl> <dl class="exception"> <dt id="RecursionError"> <em class="property">exception </em><code class="descname">RecursionError</code><a class="headerlink" href="#RecursionError" title="Permalink to this definition">¶</a></dt> <dd><p>This exception is derived from <a class="reference internal" href="#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>. It is raised when the interpreter detects that the maximum recursion depth (see <a class="reference internal" href="sys.html#sys.getrecursionlimit" title="sys.getrecursionlimit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getrecursionlimit()</span></code></a>) is exceeded.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.5: </span>Previously, a plain <a class="reference internal" href="#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> was raised.</p> </div> </dd></dl> <dl class="exception"> <dt id="ReferenceError"> <em class="property">exception </em><code class="descname">ReferenceError</code><a class="headerlink" href="#ReferenceError" title="Permalink to this definition">¶</a></dt> <dd><p>This exception is raised when a weak reference proxy, created by the <a class="reference internal" href="weakref.html#weakref.proxy" title="weakref.proxy"><code class="xref py py-func docutils literal notranslate"><span class="pre">weakref.proxy()</span></code></a> function, is used to access an attribute of the referent after it has been garbage collected. For more information on weak references, see the <a class="reference internal" href="weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> module.</p> </dd></dl> <dl class="exception"> <dt id="RuntimeError"> <em class="property">exception </em><code class="descname">RuntimeError</code><a class="headerlink" href="#RuntimeError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when an error is detected that doesn’t fall in any of the other categories. The associated value is a string indicating what precisely went wrong.</p> </dd></dl> <dl class="exception"> <dt id="StopIteration"> <em class="property">exception </em><code class="descname">StopIteration</code><a class="headerlink" href="#StopIteration" title="Permalink to this definition">¶</a></dt> <dd><p>Raised by built-in function <a class="reference internal" href="functions.html#next" title="next"><code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code></a> and an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a>’s <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> method to signal that there are no further items produced by the iterator.</p> <p>The exception object has a single attribute <code class="xref py py-attr docutils literal notranslate"><span class="pre">value</span></code>, which is given as an argument when constructing the exception, and defaults to <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.</p> <p>When a <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> or <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> function returns, a new <a class="reference internal" href="#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> instance is raised, and the value returned by the function is used as the <code class="xref py py-attr docutils literal notranslate"><span class="pre">value</span></code> parameter to the constructor of the exception.</p> <p>If a generator function defined in the presence of a <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">generator_stop</span></code> directive raises <a class="reference internal" href="#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a>, it will be converted into a <a class="reference internal" href="#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> (retaining the <a class="reference internal" href="#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> as the new exception’s cause).</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.3: </span>Added <code class="docutils literal notranslate"><span class="pre">value</span></code> attribute and the ability for generator functions to use it to return a value.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Introduced the RuntimeError transformation.</p> </div> </dd></dl> <dl class="exception"> <dt id="StopAsyncIteration"> <em class="property">exception </em><code class="descname">StopAsyncIteration</code><a class="headerlink" href="#StopAsyncIteration" title="Permalink to this definition">¶</a></dt> <dd><p>Must be raised by <a class="reference internal" href="../reference/datamodel.html#object.__anext__" title="object.__anext__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__anext__()</span></code></a> method of an <a class="reference internal" href="../glossary.html#term-asynchronous-iterator"><span class="xref std std-term">asynchronous iterator</span></a> object to stop the iteration.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.5.</span></p> </div> </dd></dl> <dl class="exception"> <dt id="SyntaxError"> <em class="property">exception </em><code class="descname">SyntaxError</code><a class="headerlink" href="#SyntaxError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when the parser encounters a syntax error. This may occur in an <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement, in a call to the built-in functions <a class="reference internal" href="functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> or <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>, or when reading the initial script or standard input (also interactively).</p> <p>Instances of this class have attributes <code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">offset</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">text</span></code> for easier access to the details. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> of the exception instance returns only the message.</p> </dd></dl> <dl class="exception"> <dt id="IndentationError"> <em class="property">exception </em><code class="descname">IndentationError</code><a class="headerlink" href="#IndentationError" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for syntax errors related to incorrect indentation. This is a subclass of <a class="reference internal" href="#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="TabError"> <em class="property">exception </em><code class="descname">TabError</code><a class="headerlink" href="#TabError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when indentation contains an inconsistent use of tabs and spaces. This is a subclass of <a class="reference internal" href="#IndentationError" title="IndentationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndentationError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="SystemError"> <em class="property">exception </em><code class="descname">SystemError</code><a class="headerlink" href="#SystemError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when the interpreter finds an internal error, but the situation does not look so serious to cause it to abandon all hope. The associated value is a string indicating what went wrong (in low-level terms).</p> <p>You should report this to the author or maintainer of your Python interpreter. Be sure to report the version of the Python interpreter (<code class="docutils literal notranslate"><span class="pre">sys.version</span></code>; it is also printed at the start of an interactive Python session), the exact error message (the exception’s associated value) and if possible the source of the program that triggered the error.</p> </dd></dl> <dl class="exception"> <dt id="SystemExit"> <em class="property">exception </em><code class="descname">SystemExit</code><a class="headerlink" href="#SystemExit" title="Permalink to this definition">¶</a></dt> <dd><p>This exception is raised by the <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a> function. It inherits from <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> instead of <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> so that it is not accidentally caught by code that catches <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>. This allows the exception to properly propagate up and cause the interpreter to exit. When it is not handled, the Python interpreter exits; no stack traceback is printed. The constructor accepts the same optional argument passed to <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a>. If the value is an integer, it specifies the system exit status (passed to C’s <code class="xref c c-func docutils literal notranslate"><span class="pre">exit()</span></code> function); if it is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the exit status is zero; if it has another type (such as a string), the object’s value is printed and the exit status is one.</p> <p>A call to <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a> is translated into an exception so that clean-up handlers (<a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> clauses of <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> statements) can be executed, and so that a debugger can execute a script without running the risk of losing control. The <a class="reference internal" href="os.html#os._exit" title="os._exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">os._exit()</span></code></a> function can be used if it is absolutely positively necessary to exit immediately (for example, in the child process after a call to <a class="reference internal" href="os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a>).</p> <dl class="attribute"> <dt id="SystemExit.code"> <code class="descname">code</code><a class="headerlink" href="#SystemExit.code" title="Permalink to this definition">¶</a></dt> <dd><p>The exit status or error message that is passed to the constructor. (Defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>.)</p> </dd></dl> </dd></dl> <dl class="exception"> <dt id="TypeError"> <em class="property">exception </em><code class="descname">TypeError</code><a class="headerlink" href="#TypeError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.</p> <p>This exception may be raised by user code to indicate that an attempted operation on an object is not supported, and is not meant to be. If an object is meant to support a given operation but has not yet provided an implementation, <a class="reference internal" href="#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> is the proper exception to raise.</p> <p>Passing arguments of the wrong type (e.g. passing a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> when an <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> is expected) should result in a <a class="reference internal" href="#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>, but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a <a class="reference internal" href="#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="UnboundLocalError"> <em class="property">exception </em><code class="descname">UnboundLocalError</code><a class="headerlink" href="#UnboundLocalError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable. This is a subclass of <a class="reference internal" href="#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="UnicodeError"> <em class="property">exception </em><code class="descname">UnicodeError</code><a class="headerlink" href="#UnicodeError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a Unicode-related encoding or decoding error occurs. It is a subclass of <a class="reference internal" href="#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p> <p><a class="reference internal" href="#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a> has attributes that describe the encoding or decoding error. For example, <code class="docutils literal notranslate"><span class="pre">err.object[err.start:err.end]</span></code> gives the particular invalid input that the codec failed on.</p> <dl class="attribute"> <dt id="UnicodeError.encoding"> <code class="descname">encoding</code><a class="headerlink" href="#UnicodeError.encoding" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the encoding that raised the error.</p> </dd></dl> <dl class="attribute"> <dt id="UnicodeError.reason"> <code class="descname">reason</code><a class="headerlink" href="#UnicodeError.reason" title="Permalink to this definition">¶</a></dt> <dd><p>A string describing the specific codec error.</p> </dd></dl> <dl class="attribute"> <dt id="UnicodeError.object"> <code class="descname">object</code><a class="headerlink" href="#UnicodeError.object" title="Permalink to this definition">¶</a></dt> <dd><p>The object the codec was attempting to encode or decode.</p> </dd></dl> <dl class="attribute"> <dt id="UnicodeError.start"> <code class="descname">start</code><a class="headerlink" href="#UnicodeError.start" title="Permalink to this definition">¶</a></dt> <dd><p>The first index of invalid data in <a class="reference internal" href="functions.html#object" title="object"><code class="xref py py-attr docutils literal notranslate"><span class="pre">object</span></code></a>.</p> </dd></dl> <dl class="attribute"> <dt id="UnicodeError.end"> <code class="descname">end</code><a class="headerlink" href="#UnicodeError.end" title="Permalink to this definition">¶</a></dt> <dd><p>The index after the last invalid data in <a class="reference internal" href="functions.html#object" title="object"><code class="xref py py-attr docutils literal notranslate"><span class="pre">object</span></code></a>.</p> </dd></dl> </dd></dl> <dl class="exception"> <dt id="UnicodeEncodeError"> <em class="property">exception </em><code class="descname">UnicodeEncodeError</code><a class="headerlink" href="#UnicodeEncodeError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a Unicode-related error occurs during encoding. It is a subclass of <a class="reference internal" href="#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="UnicodeDecodeError"> <em class="property">exception </em><code class="descname">UnicodeDecodeError</code><a class="headerlink" href="#UnicodeDecodeError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a Unicode-related error occurs during decoding. It is a subclass of <a class="reference internal" href="#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="UnicodeTranslateError"> <em class="property">exception </em><code class="descname">UnicodeTranslateError</code><a class="headerlink" href="#UnicodeTranslateError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a Unicode-related error occurs during translating. It is a subclass of <a class="reference internal" href="#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="ValueError"> <em class="property">exception </em><code class="descname">ValueError</code><a class="headerlink" href="#ValueError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when an operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as <a class="reference internal" href="#IndexError" title="IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="ZeroDivisionError"> <em class="property">exception </em><code class="descname">ZeroDivisionError</code><a class="headerlink" href="#ZeroDivisionError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when the second argument of a division or modulo operation is zero. The associated value is a string indicating the type of the operands and the operation.</p> </dd></dl> <p>The following exceptions are kept for compatibility with previous versions; starting from Python 3.3, they are aliases of <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>.</p> <dl class="exception"> <dt id="EnvironmentError"> <em class="property">exception </em><code class="descname">EnvironmentError</code><a class="headerlink" href="#EnvironmentError" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="exception"> <dt id="IOError"> <em class="property">exception </em><code class="descname">IOError</code><a class="headerlink" href="#IOError" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="exception"> <dt id="WindowsError"> <em class="property">exception </em><code class="descname">WindowsError</code><a class="headerlink" href="#WindowsError" title="Permalink to this definition">¶</a></dt> <dd><p>Only available on Windows.</p> </dd></dl> <div class="section" id="os-exceptions"> <h3>5.2.1. OS exceptions<a class="headerlink" href="#os-exceptions" title="Permalink to this headline">¶</a></h3> <p>The following exceptions are subclasses of <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>, they get raised depending on the system error code.</p> <dl class="exception"> <dt id="BlockingIOError"> <em class="property">exception </em><code class="descname">BlockingIOError</code><a class="headerlink" href="#BlockingIOError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when an operation would block on an object (e.g. socket) set for non-blocking operation. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">EAGAIN</span></code>, <code class="docutils literal notranslate"><span class="pre">EALREADY</span></code>, <code class="docutils literal notranslate"><span class="pre">EWOULDBLOCK</span></code> and <code class="docutils literal notranslate"><span class="pre">EINPROGRESS</span></code>.</p> <p>In addition to those of <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>, <a class="reference internal" href="#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a> can have one more attribute:</p> <dl class="attribute"> <dt id="BlockingIOError.characters_written"> <code class="descname">characters_written</code><a class="headerlink" href="#BlockingIOError.characters_written" title="Permalink to this definition">¶</a></dt> <dd><p>An integer containing the number of characters written to the stream before it blocked. This attribute is available when using the buffered I/O classes from the <a class="reference internal" href="io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> module.</p> </dd></dl> </dd></dl> <dl class="exception"> <dt id="ChildProcessError"> <em class="property">exception </em><code class="descname">ChildProcessError</code><a class="headerlink" href="#ChildProcessError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when an operation on a child process failed. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">ECHILD</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="ConnectionError"> <em class="property">exception </em><code class="descname">ConnectionError</code><a class="headerlink" href="#ConnectionError" title="Permalink to this definition">¶</a></dt> <dd><p>A base class for connection-related issues.</p> <p>Subclasses are <a class="reference internal" href="#BrokenPipeError" title="BrokenPipeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenPipeError</span></code></a>, <a class="reference internal" href="#ConnectionAbortedError" title="ConnectionAbortedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionAbortedError</span></code></a>, <a class="reference internal" href="#ConnectionRefusedError" title="ConnectionRefusedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionRefusedError</span></code></a> and <a class="reference internal" href="#ConnectionResetError" title="ConnectionResetError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionResetError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="BrokenPipeError"> <em class="property">exception </em><code class="descname">BrokenPipeError</code><a class="headerlink" href="#BrokenPipeError" title="Permalink to this definition">¶</a></dt> <dd><p>A subclass of <a class="reference internal" href="#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a>, raised when trying to write on a pipe while the other end has been closed, or trying to write on a socket which has been shutdown for writing. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">EPIPE</span></code> and <code class="docutils literal notranslate"><span class="pre">ESHUTDOWN</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="ConnectionAbortedError"> <em class="property">exception </em><code class="descname">ConnectionAbortedError</code><a class="headerlink" href="#ConnectionAbortedError" title="Permalink to this definition">¶</a></dt> <dd><p>A subclass of <a class="reference internal" href="#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a>, raised when a connection attempt is aborted by the peer. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">ECONNABORTED</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="ConnectionRefusedError"> <em class="property">exception </em><code class="descname">ConnectionRefusedError</code><a class="headerlink" href="#ConnectionRefusedError" title="Permalink to this definition">¶</a></dt> <dd><p>A subclass of <a class="reference internal" href="#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a>, raised when a connection attempt is refused by the peer. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">ECONNREFUSED</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="ConnectionResetError"> <em class="property">exception </em><code class="descname">ConnectionResetError</code><a class="headerlink" href="#ConnectionResetError" title="Permalink to this definition">¶</a></dt> <dd><p>A subclass of <a class="reference internal" href="#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a>, raised when a connection is reset by the peer. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">ECONNRESET</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="FileExistsError"> <em class="property">exception </em><code class="descname">FileExistsError</code><a class="headerlink" href="#FileExistsError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when trying to create a file or directory which already exists. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">EEXIST</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="FileNotFoundError"> <em class="property">exception </em><code class="descname">FileNotFoundError</code><a class="headerlink" href="#FileNotFoundError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a file or directory is requested but doesn’t exist. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">ENOENT</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="InterruptedError"> <em class="property">exception </em><code class="descname">InterruptedError</code><a class="headerlink" href="#InterruptedError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a system call is interrupted by an incoming signal. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.EINTR" title="errno.EINTR"><code class="xref py py-data docutils literal notranslate"><span class="pre">EINTR</span></code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Python now retries system calls when a syscall is interrupted by a signal, except if the signal handler raises an exception (see <span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0475"><strong>PEP 475</strong></a> for the rationale), instead of raising <a class="reference internal" href="#InterruptedError" title="InterruptedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InterruptedError</span></code></a>.</p> </div> </dd></dl> <dl class="exception"> <dt id="IsADirectoryError"> <em class="property">exception </em><code class="descname">IsADirectoryError</code><a class="headerlink" href="#IsADirectoryError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a file operation (such as <a class="reference internal" href="os.html#os.remove" title="os.remove"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.remove()</span></code></a>) is requested on a directory. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">EISDIR</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="NotADirectoryError"> <em class="property">exception </em><code class="descname">NotADirectoryError</code><a class="headerlink" href="#NotADirectoryError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a directory operation (such as <a class="reference internal" href="os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a>) is requested on something which is not a directory. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">ENOTDIR</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="PermissionError"> <em class="property">exception </em><code class="descname">PermissionError</code><a class="headerlink" href="#PermissionError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when trying to run an operation without the adequate access rights - for example filesystem permissions. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">EACCES</span></code> and <code class="docutils literal notranslate"><span class="pre">EPERM</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="ProcessLookupError"> <em class="property">exception </em><code class="descname">ProcessLookupError</code><a class="headerlink" href="#ProcessLookupError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a given process doesn’t exist. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">ESRCH</span></code>.</p> </dd></dl> <dl class="exception"> <dt id="TimeoutError"> <em class="property">exception </em><code class="descname">TimeoutError</code><a class="headerlink" href="#TimeoutError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when a system function timed out at the system level. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <code class="docutils literal notranslate"><span class="pre">ETIMEDOUT</span></code>.</p> </dd></dl> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3: </span>All the above <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> subclasses were added.</p> </div> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3151"><strong>PEP 3151</strong></a> - Reworking the OS and IO exception hierarchy</p> </div> </div> </div> <div class="section" id="warnings"> <h2>5.3. Warnings<a class="headerlink" href="#warnings" title="Permalink to this headline">¶</a></h2> <p>The following exceptions are used as warning categories; see the <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> module for more information.</p> <dl class="exception"> <dt id="Warning"> <em class="property">exception </em><code class="descname">Warning</code><a class="headerlink" href="#Warning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warning categories.</p> </dd></dl> <dl class="exception"> <dt id="UserWarning"> <em class="property">exception </em><code class="descname">UserWarning</code><a class="headerlink" href="#UserWarning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warnings generated by user code.</p> </dd></dl> <dl class="exception"> <dt id="DeprecationWarning"> <em class="property">exception </em><code class="descname">DeprecationWarning</code><a class="headerlink" href="#DeprecationWarning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warnings about deprecated features.</p> </dd></dl> <dl class="exception"> <dt id="PendingDeprecationWarning"> <em class="property">exception </em><code class="descname">PendingDeprecationWarning</code><a class="headerlink" href="#PendingDeprecationWarning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warnings about features which will be deprecated in the future.</p> </dd></dl> <dl class="exception"> <dt id="SyntaxWarning"> <em class="property">exception </em><code class="descname">SyntaxWarning</code><a class="headerlink" href="#SyntaxWarning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warnings about dubious syntax.</p> </dd></dl> <dl class="exception"> <dt id="RuntimeWarning"> <em class="property">exception </em><code class="descname">RuntimeWarning</code><a class="headerlink" href="#RuntimeWarning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warnings about dubious runtime behavior.</p> </dd></dl> <dl class="exception"> <dt id="FutureWarning"> <em class="property">exception </em><code class="descname">FutureWarning</code><a class="headerlink" href="#FutureWarning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warnings about constructs that will change semantically in the future.</p> </dd></dl> <dl class="exception"> <dt id="ImportWarning"> <em class="property">exception </em><code class="descname">ImportWarning</code><a class="headerlink" href="#ImportWarning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warnings about probable mistakes in module imports.</p> </dd></dl> <dl class="exception"> <dt id="UnicodeWarning"> <em class="property">exception </em><code class="descname">UnicodeWarning</code><a class="headerlink" href="#UnicodeWarning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warnings related to Unicode.</p> </dd></dl> <dl class="exception"> <dt id="BytesWarning"> <em class="property">exception </em><code class="descname">BytesWarning</code><a class="headerlink" href="#BytesWarning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warnings related to <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> and <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="ResourceWarning"> <em class="property">exception </em><code class="descname">ResourceWarning</code><a class="headerlink" href="#ResourceWarning" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for warnings related to resource usage.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.2.</span></p> </div> </dd></dl> </div> <div class="section" id="exception-hierarchy"> <h2>5.4. Exception hierarchy<a class="headerlink" href="#exception-hierarchy" title="Permalink to this headline">¶</a></h2> <p>The class hierarchy for built-in exceptions is:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ne">BaseException</span> <span class="o">+--</span> <span class="ne">SystemExit</span> <span class="o">+--</span> <span class="ne">KeyboardInterrupt</span> <span class="o">+--</span> <span class="ne">GeneratorExit</span> <span class="o">+--</span> <span class="ne">Exception</span> <span class="o">+--</span> <span class="ne">StopIteration</span> <span class="o">+--</span> <span class="n">StopAsyncIteration</span> <span class="o">+--</span> <span class="ne">ArithmeticError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">FloatingPointError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">OverflowError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">ZeroDivisionError</span> <span class="o">+--</span> <span class="ne">AssertionError</span> <span class="o">+--</span> <span class="ne">AttributeError</span> <span class="o">+--</span> <span class="ne">BufferError</span> <span class="o">+--</span> <span class="ne">EOFError</span> <span class="o">+--</span> <span class="ne">ImportError</span> <span class="o">|</span> <span class="o">+--</span> <span class="n">ModuleNotFoundError</span> <span class="o">+--</span> <span class="ne">LookupError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">IndexError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">KeyError</span> <span class="o">+--</span> <span class="ne">MemoryError</span> <span class="o">+--</span> <span class="ne">NameError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">UnboundLocalError</span> <span class="o">+--</span> <span class="ne">OSError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">BlockingIOError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">ChildProcessError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">ConnectionError</span> <span class="o">|</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">BrokenPipeError</span> <span class="o">|</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">ConnectionAbortedError</span> <span class="o">|</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">ConnectionRefusedError</span> <span class="o">|</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">ConnectionResetError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">FileExistsError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">FileNotFoundError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">InterruptedError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">IsADirectoryError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">NotADirectoryError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">PermissionError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">ProcessLookupError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">TimeoutError</span> <span class="o">+--</span> <span class="ne">ReferenceError</span> <span class="o">+--</span> <span class="ne">RuntimeError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">NotImplementedError</span> <span class="o">|</span> <span class="o">+--</span> <span class="n">RecursionError</span> <span class="o">+--</span> <span class="ne">SyntaxError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">IndentationError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">TabError</span> <span class="o">+--</span> <span class="ne">SystemError</span> <span class="o">+--</span> <span class="ne">TypeError</span> <span class="o">+--</span> <span class="ne">ValueError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">UnicodeError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">UnicodeDecodeError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">UnicodeEncodeError</span> <span class="o">|</span> <span class="o">+--</span> <span class="ne">UnicodeTranslateError</span> <span class="o">+--</span> <span class="ne">Warning</span> <span class="o">+--</span> <span class="ne">DeprecationWarning</span> <span class="o">+--</span> <span class="ne">PendingDeprecationWarning</span> <span class="o">+--</span> <span class="ne">RuntimeWarning</span> <span class="o">+--</span> <span class="ne">SyntaxWarning</span> <span class="o">+--</span> <span class="ne">UserWarning</span> <span class="o">+--</span> <span class="ne">FutureWarning</span> <span class="o">+--</span> <span class="ne">ImportWarning</span> <span class="o">+--</span> <span class="ne">UnicodeWarning</span> <span class="o">+--</span> <span class="ne">BytesWarning</span> <span class="o">+--</span> <span class="ne">ResourceWarning</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">5. Built-in Exceptions</a><ul> <li><a class="reference internal" href="#base-classes">5.1. Base classes</a></li> <li><a class="reference internal" href="#concrete-exceptions">5.2. Concrete exceptions</a><ul> <li><a class="reference internal" href="#os-exceptions">5.2.1. OS exceptions</a></li> </ul> </li> <li><a class="reference internal" href="#warnings">5.3. Warnings</a></li> <li><a class="reference internal" href="#exception-hierarchy">5.4. Exception hierarchy</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="stdtypes.html" title="previous chapter">4. Built-in Types</a></p> <h4>Next topic</h4> <p class="topless"><a href="text.html" title="next chapter">6. Text Processing Services</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.6/Doc/library/exceptions.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="text.html" title="6. Text Processing Services" >next</a> |</li> <li class="right" > <a href="stdtypes.html" title="4. Built-in Types" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">3.6.7 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="right"> <div class="inline-search" style="display: none" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('.inline-search').show(0);</script> | </li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Dec 18, 2023. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.6. </div> </body> </html>
Save
cmd:
run