| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Exceptions} | 
 | 2 |  | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 3 | Exceptions can be class objects or string objects.  While | 
 | 4 | traditionally, most exceptions have been string objects, in Python | 
 | 5 | 1.5a4, all standard exceptions have been converted to class objects, | 
 | 6 | and users are encouraged to the the same.  The source code for those | 
 | 7 | exceptions is present in the standard library module | 
 | 8 | \code{exceptions}; this module never needs to be imported explicitly. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 9 |  | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 10 | For backward compatibility, when Python is invoked with the \code{-X} | 
 | 11 | option, the standard exceptions are strings.  This may be needed to | 
 | 12 | run some code that breaks because of the different semantics of class | 
 | 13 | based exceptions.  The \code{-X} option will become obsolete in future | 
 | 14 | Python versions, so the recommended solution is to fix the code. | 
 | 15 |  | 
 | 16 | Two distinct string objects with the same value are considered different | 
 | 17 | exceptions.  This is done to force programmers to use exception names | 
 | 18 | rather than their string value when specifying exception handlers. | 
 | 19 | The string value of all built-in exceptions is their name, but this is | 
 | 20 | not a requirement for user-defined exceptions or exceptions defined by | 
 | 21 | library modules. | 
 | 22 |  | 
 | 23 | For class exceptions, in a \code{try} statement with an\code{except} | 
 | 24 | clause that mentions a particular class, that clause also handles | 
 | 25 | any exception classes derived from that class (but not exception | 
 | 26 | classes from which \emph{it} is derived).  Two exception classes | 
 | 27 | that are not related via subclassing are never equivalent, even if | 
 | 28 | they have the same name. | 
 | 29 | \stindex{try} | 
 | 30 | \stindex{except} | 
 | 31 |  | 
 | 32 | The built-in exceptions listed below can be generated by the | 
 | 33 | interpreter or built-in functions.  Except where mentioned, they have | 
 | 34 | an ``associated value'' indicating the detailed cause of the error. | 
 | 35 | This may be a string or a tuple containing several items of | 
 | 36 | information (e.g., an error code and a string explaining the code). | 
 | 37 | The associated value is the second argument to the \code{raise} | 
 | 38 | statement.  For string exceptions, the associated value itself will be | 
 | 39 | stored in the variable named as the second argument of the | 
 | 40 | \code{except} clause (if any).  For class exceptions derived from | 
 | 41 | the root class \code{Exception}, that variable receives the exception | 
 | 42 | instance, and the associated value is present as the exception | 
 | 43 | instance's \code{args} attribute; this is a tuple even if the second | 
 | 44 | argument to \code{raise} was not (then it is a singleton tuple). | 
 | 45 | \stindex{raise} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 46 |  | 
 | 47 | User code can raise built-in exceptions.  This can be used to test an | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 48 | exception handler or to report an error condition ``just like'' the | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 49 | situation in which the interpreter raises the same exception; but | 
 | 50 | beware that there is nothing to prevent user code from raising an | 
 | 51 | inappropriate error. | 
 | 52 |  | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 53 | \renewcommand{\indexsubitem}{(built-in exception base class)} | 
 | 54 |  | 
 | 55 | The following exceptions are only used as base classes for other | 
 | 56 | exceptions.  When string-based standard exceptions are used, they | 
 | 57 | are tuples containing the directly derived classes. | 
 | 58 |  | 
 | 59 | \begin{excdesc}{Exception} | 
 | 60 | The root class for exceptions.  All built-in exceptions are derived | 
 | 61 | from this class.  All user-defined exceptions should also be derived | 
 | 62 | from this class, but this is not (yet) enforced.  The \code{str()} | 
 | 63 | function, when applied to an instance of this class (or most derived | 
 | 64 | classes) returns the string value of the argument or arguments, or an | 
| Guido van Rossum | 6cd7ecb | 1997-10-07 14:41:04 +0000 | [diff] [blame] | 65 | empty string if no arguments were given to the constructor.  When used | 
 | 66 | as a sequence, this accesses the arguments given to the constructor | 
 | 67 | (handy for backward compatibility with old code). | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 68 | \end{excdesc} | 
 | 69 |  | 
 | 70 | \begin{excdesc}{StandardError} | 
 | 71 | The base class for built-in exceptions.  All built-in exceptions are | 
 | 72 | derived from this class, which is itself derived from the root class | 
| Guido van Rossum | 6cd7ecb | 1997-10-07 14:41:04 +0000 | [diff] [blame] | 73 | \code{Exception}. | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 74 | \end{excdesc} | 
 | 75 |  | 
 | 76 | \begin{excdesc}{ArithmeticError} | 
 | 77 | The base class for those built-in exceptions that are raised for | 
 | 78 | various arithmetic errors: \code{OverflowError}, | 
 | 79 | \code{ZeroDivisionError}, \code{FloatingPointError}. | 
 | 80 | \end{excdesc} | 
 | 81 |  | 
 | 82 | \begin{excdesc}{LookupError} | 
 | 83 | The base class for thise exceptions that are raised when a key or | 
 | 84 | index used on a mapping or sequence is invalid: \code{IndexError}, | 
 | 85 | \code{KeyError}. | 
 | 86 | \end{excdesc} | 
 | 87 |  | 
 | 88 | \renewcommand{\indexsubitem}{(built-in exception base class)} | 
 | 89 |  | 
 | 90 | The following exceptions are the exceptions that are actually raised. | 
 | 91 | They are class objects, except when the \code{-X} option is used to | 
 | 92 | revert back to string-based standard exceptions. | 
 | 93 |  | 
 | 94 | \begin{excdesc}{AssertionError} | 
 | 95 | Raised when an \code{assert} statement fails. | 
 | 96 | \stindex{assert} | 
 | 97 | \end{excdesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 98 |  | 
 | 99 | \begin{excdesc}{AttributeError} | 
 | 100 | % xref to attribute reference? | 
 | 101 |   Raised when an attribute reference or assignment fails.  (When an | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 102 |   object does not support attribute references or attribute assignments | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 103 |   at all, \code{TypeError} is raised.) | 
 | 104 | \end{excdesc} | 
 | 105 |  | 
 | 106 | \begin{excdesc}{EOFError} | 
 | 107 | % XXXJH xrefs here | 
 | 108 |   Raised when one of the built-in functions (\code{input()} or | 
 | 109 |   \code{raw_input()}) hits an end-of-file condition (\EOF{}) without | 
 | 110 |   reading any data. | 
 | 111 | % XXXJH xrefs here | 
 | 112 |   (N.B.: the \code{read()} and \code{readline()} methods of file | 
 | 113 |   objects return an empty string when they hit \EOF{}.)  No associated value. | 
 | 114 | \end{excdesc} | 
 | 115 |  | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 116 | \begin{excdesc}{FloatingPointError} | 
 | 117 | Raised when a floating point operation fails.  This exception is | 
 | 118 | always defined, but can only be raised when Python is configured with | 
 | 119 | the \code{--with-fpectl} option, or the \code{WANT_SIGFPE_HANDLER} | 
 | 120 | symbol is defined in the \file{config.h} file. | 
 | 121 | \end{excdesc} | 
 | 122 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 123 | \begin{excdesc}{IOError} | 
 | 124 | % XXXJH xrefs here | 
 | 125 |   Raised when an I/O operation (such as a \code{print} statement, the | 
 | 126 |   built-in \code{open()} function or a method of a file object) fails | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 127 |   for an I/O-related reason, e.g., ``file not found'' or ``disk full''. | 
 | 128 |  | 
 | 129 | When class exceptions are used, and this exception is instantiated as | 
 | 130 | \code{IOError(errno, strerror)}, the instance has two additional | 
 | 131 | attributes \code{errno} and \code{strerror} set to the error code and | 
 | 132 | the error message, respectively.  These attributes default to | 
 | 133 | \code{None}. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 134 | \end{excdesc} | 
 | 135 |  | 
 | 136 | \begin{excdesc}{ImportError} | 
 | 137 | % XXXJH xref to import statement? | 
 | 138 |   Raised when an \code{import} statement fails to find the module | 
 | 139 |   definition or when a \code{from {\rm \ldots} import} fails to find a | 
 | 140 |   name that is to be imported. | 
 | 141 | \end{excdesc} | 
 | 142 |  | 
 | 143 | \begin{excdesc}{IndexError} | 
 | 144 | % XXXJH xref to sequences | 
 | 145 |   Raised when a sequence subscript is out of range.  (Slice indices are | 
 | 146 |   silently truncated to fall in the allowed range; if an index is not a | 
 | 147 |   plain integer, \code{TypeError} is raised.) | 
 | 148 | \end{excdesc} | 
 | 149 |  | 
 | 150 | \begin{excdesc}{KeyError} | 
 | 151 | % XXXJH xref to mapping objects? | 
 | 152 |   Raised when a mapping (dictionary) key is not found in the set of | 
 | 153 |   existing keys. | 
 | 154 | \end{excdesc} | 
 | 155 |  | 
 | 156 | \begin{excdesc}{KeyboardInterrupt} | 
 | 157 |   Raised when the user hits the interrupt key (normally | 
 | 158 |   \kbd{Control-C} or | 
 | 159 | \key{DEL}).  During execution, a check for interrupts is made regularly. | 
 | 160 | % XXXJH xrefs here | 
 | 161 |   Interrupts typed when a built-in function \code{input()} or | 
 | 162 |   \code{raw_input()}) is waiting for input also raise this exception.  No | 
 | 163 |   associated value. | 
 | 164 | \end{excdesc} | 
 | 165 |  | 
 | 166 | \begin{excdesc}{MemoryError} | 
 | 167 |   Raised when an operation runs out of memory but the situation may | 
 | 168 |   still be rescued (by deleting some objects).  The associated value is | 
 | 169 |   a string indicating what kind of (internal) operation ran out of memory. | 
 | 170 |   Note that because of the underlying memory management architecture | 
 | 171 |   (\C{}'s \code{malloc()} function), the interpreter may not always be able | 
 | 172 |   to completely recover from this situation; it nevertheless raises an | 
 | 173 |   exception so that a stack traceback can be printed, in case a run-away | 
 | 174 |   program was the cause. | 
 | 175 | \end{excdesc} | 
 | 176 |  | 
 | 177 | \begin{excdesc}{NameError} | 
 | 178 |   Raised when a local or global name is not found.  This applies only | 
 | 179 |   to unqualified names.  The associated value is the name that could | 
 | 180 |   not be found. | 
 | 181 | \end{excdesc} | 
 | 182 |  | 
 | 183 | \begin{excdesc}{OverflowError} | 
 | 184 | % XXXJH reference to long's and/or int's? | 
 | 185 |   Raised when the result of an arithmetic operation is too large to be | 
 | 186 |   represented.  This cannot occur for long integers (which would rather | 
 | 187 |   raise \code{MemoryError} than give up).  Because of the lack of | 
 | 188 |   standardization of floating point exception handling in \C{}, most | 
 | 189 |   floating point operations also aren't checked.  For plain integers, | 
 | 190 |   all operations that can overflow are checked except left shift, where | 
 | 191 |   typical applications prefer to drop bits than raise an exception. | 
 | 192 | \end{excdesc} | 
 | 193 |  | 
 | 194 | \begin{excdesc}{RuntimeError} | 
 | 195 |   Raised when an error is detected that doesn't fall in any of the | 
 | 196 |   other categories.  The associated value is a string indicating what | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 197 |   precisely went wrong.  (This exception is mostly a relic from a | 
 | 198 |   previous version of the interpreter; it is not used very much any | 
 | 199 |   more.) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 200 | \end{excdesc} | 
 | 201 |  | 
 | 202 | \begin{excdesc}{SyntaxError} | 
 | 203 | % XXXJH xref to these functions? | 
 | 204 |   Raised when the parser encounters a syntax error.  This may occur in | 
 | 205 |   an \code{import} statement, in an \code{exec} statement, in a call | 
 | 206 |   to the built-in function \code{eval()} or \code{input()}, or | 
 | 207 |   when reading the initial script or standard input (also | 
 | 208 |   interactively). | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 209 |  | 
 | 210 | When class exceptions are used, instances of this class have | 
 | 211 | atttributes \code{filename}, \code{lineno}, \code{offset} and | 
 | 212 | \code{text} for easier access to the details; for string exceptions, | 
 | 213 | the associated value is usually a tuple of the form | 
 | 214 | \code{(message, (filename, lineno, offset, text))}. | 
 | 215 | For class exceptions, \code{str()} returns only the message. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 216 | \end{excdesc} | 
 | 217 |  | 
 | 218 | \begin{excdesc}{SystemError} | 
 | 219 |   Raised when the interpreter finds an internal error, but the | 
 | 220 |   situation does not look so serious to cause it to abandon all hope. | 
 | 221 |   The associated value is a string indicating what went wrong (in | 
 | 222 |   low-level terms). | 
 | 223 |    | 
 | 224 |   You should report this to the author or maintainer of your Python | 
 | 225 |   interpreter.  Be sure to report the version string of the Python | 
 | 226 |   interpreter (\code{sys.version}; it is also printed at the start of an | 
 | 227 |   interactive Python session), the exact error message (the exception's | 
 | 228 |   associated value) and if possible the source of the program that | 
 | 229 |   triggered the error. | 
 | 230 | \end{excdesc} | 
 | 231 |  | 
 | 232 | \begin{excdesc}{SystemExit} | 
 | 233 | % XXXJH xref to module sys? | 
 | 234 |   This exception is raised by the \code{sys.exit()} function.  When it | 
 | 235 |   is not handled, the Python interpreter exits; no stack traceback is | 
 | 236 |   printed.  If the associated value is a plain integer, it specifies the | 
 | 237 |   system exit status (passed to \C{}'s \code{exit()} function); if it is | 
 | 238 |   \code{None}, the exit status is zero; if it has another type (such as | 
 | 239 |   a string), the object's value is printed and the exit status is one. | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 240 |  | 
 | 241 | When class exceptions are used, the instance has an attribute | 
 | 242 | \code{code} which is set to the proposed exit status or error message | 
 | 243 | (defaulting to \code{None}). | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 244 |    | 
 | 245 |   A call to \code{sys.exit} is translated into an exception so that | 
 | 246 |   clean-up handlers (\code{finally} clauses of \code{try} statements) | 
 | 247 |   can be executed, and so that a debugger can execute a script without | 
| Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 248 |   running the risk of losing control.  The \code{os._exit()} function | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 249 |   can be used if it is absolutely positively necessary to exit | 
 | 250 |   immediately (e.g., after a \code{fork()} in the child process). | 
 | 251 | \end{excdesc} | 
 | 252 |  | 
 | 253 | \begin{excdesc}{TypeError} | 
 | 254 |   Raised when a built-in operation or function is applied to an object | 
 | 255 |   of inappropriate type.  The associated value is a string giving | 
 | 256 |   details about the type mismatch. | 
 | 257 | \end{excdesc} | 
 | 258 |  | 
 | 259 | \begin{excdesc}{ValueError} | 
 | 260 |   Raised when a built-in operation or function receives an argument | 
 | 261 |   that has the right type but an inappropriate value, and the | 
 | 262 |   situation is not described by a more precise exception such as | 
 | 263 |   \code{IndexError}. | 
 | 264 | \end{excdesc} | 
 | 265 |  | 
 | 266 | \begin{excdesc}{ZeroDivisionError} | 
 | 267 |   Raised when the second argument of a division or modulo operation is | 
 | 268 |   zero.  The associated value is a string indicating the type of the | 
 | 269 |   operands and the operation. | 
 | 270 | \end{excdesc} |