Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Exceptions} |
Fred Drake | 3bd9ab0 | 1998-07-23 19:33:08 +0000 | [diff] [blame] | 2 | |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{exceptions} |
Fred Drake | 3bd9ab0 | 1998-07-23 19:33:08 +0000 | [diff] [blame] | 4 | \modulesynopsis{Standard exceptions classes.} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 5 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 6 | |
Fred Drake | 7acb218 | 2000-09-09 03:28:00 +0000 | [diff] [blame] | 7 | Exceptions can be class objects or string objects. Though most |
| 8 | exceptions have been string objects in past versions of Python, in |
| 9 | Python 1.5 and newer versions, all standard exceptions have been |
| 10 | converted to class objects, and users are encouraged to do the same. |
| 11 | The exceptions are defined in the module \module{exceptions}. This |
| 12 | module never needs to be imported explicitly: the exceptions are |
| 13 | provided in the built-in namespace. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 15 | Two distinct string objects with the same value are considered different |
| 16 | exceptions. This is done to force programmers to use exception names |
| 17 | rather than their string value when specifying exception handlers. |
| 18 | The string value of all built-in exceptions is their name, but this is |
| 19 | not a requirement for user-defined exceptions or exceptions defined by |
| 20 | library modules. |
| 21 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 22 | For class exceptions, in a \keyword{try}\stindex{try} statement with |
| 23 | an \keyword{except}\stindex{except} clause that mentions a particular |
| 24 | class, that clause also handles any exception classes derived from |
| 25 | that class (but not exception classes from which \emph{it} is |
| 26 | derived). Two exception classes that are not related via subclassing |
| 27 | are never equivalent, even if they have the same name. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 28 | |
| 29 | The built-in exceptions listed below can be generated by the |
| 30 | interpreter or built-in functions. Except where mentioned, they have |
| 31 | an ``associated value'' indicating the detailed cause of the error. |
| 32 | This may be a string or a tuple containing several items of |
| 33 | information (e.g., an error code and a string explaining the code). |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 34 | The associated value is the second argument to the |
| 35 | \keyword{raise}\stindex{raise} statement. For string exceptions, the |
| 36 | associated value itself will be stored in the variable named as the |
| 37 | second argument of the \keyword{except} clause (if any). For class |
| 38 | exceptions, that variable receives the exception instance. If the |
| 39 | exception class is derived from the standard root class |
| 40 | \exception{Exception}, the associated value is present as the |
| 41 | exception instance's \member{args} attribute, and possibly on other |
| 42 | attributes as well. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 43 | |
| 44 | 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] | 45 | exception handler or to report an error condition ``just like'' the |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 46 | situation in which the interpreter raises the same exception; but |
| 47 | beware that there is nothing to prevent user code from raising an |
| 48 | inappropriate error. |
| 49 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 50 | \setindexsubitem{(built-in exception base class)} |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 51 | |
| 52 | The following exceptions are only used as base classes for other |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 53 | exceptions. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 54 | |
| 55 | \begin{excdesc}{Exception} |
| 56 | The root class for exceptions. All built-in exceptions are derived |
| 57 | from this class. All user-defined exceptions should also be derived |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 58 | from this class, but this is not (yet) enforced. The \function{str()} |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 59 | function, when applied to an instance of this class (or most derived |
| 60 | 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] | 61 | empty string if no arguments were given to the constructor. When used |
| 62 | as a sequence, this accesses the arguments given to the constructor |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 63 | (handy for backward compatibility with old code). The arguments are |
Fred Drake | c457ca7 | 1998-07-23 20:31:53 +0000 | [diff] [blame] | 64 | also available on the instance's \member{args} attribute, as a tuple. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 65 | \end{excdesc} |
| 66 | |
| 67 | \begin{excdesc}{StandardError} |
Barry Warsaw | f2b4554 | 1999-02-24 00:27:14 +0000 | [diff] [blame] | 68 | The base class for all built-in exceptions except |
| 69 | \exception{SystemExit}. \exception{StandardError} itself is derived |
| 70 | from the root class |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 71 | \exception{Exception}. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 72 | \end{excdesc} |
| 73 | |
| 74 | \begin{excdesc}{ArithmeticError} |
| 75 | The base class for those built-in exceptions that are raised for |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 76 | various arithmetic errors: \exception{OverflowError}, |
| 77 | \exception{ZeroDivisionError}, \exception{FloatingPointError}. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 78 | \end{excdesc} |
| 79 | |
| 80 | \begin{excdesc}{LookupError} |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 81 | The base class for the exceptions that are raised when a key or |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 82 | index used on a mapping or sequence is invalid: \exception{IndexError}, |
Fred Drake | 53143be | 2000-10-25 21:05:29 +0000 | [diff] [blame] | 83 | \exception{KeyError}. This can be raised directly by |
| 84 | \function{sys.setdefaultencoding()}. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 85 | \end{excdesc} |
| 86 | |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 87 | \begin{excdesc}{EnvironmentError} |
| 88 | The base class for exceptions that |
| 89 | can occur outside the Python system: \exception{IOError}, |
| 90 | \exception{OSError}. When exceptions of this type are created with a |
| 91 | 2-tuple, the first item is available on the instance's \member{errno} |
| 92 | attribute (it is assumed to be an error number), and the second item |
| 93 | is available on the \member{strerror} attribute (it is usually the |
| 94 | associated error message). The tuple itself is also available on the |
| 95 | \member{args} attribute. |
Fred Drake | d0bceee | 1999-02-02 18:00:40 +0000 | [diff] [blame] | 96 | \versionadded{1.5.2} |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 97 | |
| 98 | When an \exception{EnvironmentError} exception is instantiated with a |
| 99 | 3-tuple, the first two items are available as above, while the third |
| 100 | item is available on the \member{filename} attribute. However, for |
| 101 | backwards compatibility, the \member{args} attribute contains only a |
| 102 | 2-tuple of the first two constructor arguments. |
| 103 | |
| 104 | The \member{filename} attribute is \code{None} when this exception is |
| 105 | created with other than 3 arguments. The \member{errno} and |
| 106 | \member{strerror} attributes are also \code{None} when the instance was |
| 107 | created with other than 2 or 3 arguments. In this last case, |
| 108 | \member{args} contains the verbatim constructor arguments as a tuple. |
| 109 | \end{excdesc} |
| 110 | |
Fred Drake | 88c023b | 2000-09-07 16:33:32 +0000 | [diff] [blame] | 111 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 112 | \setindexsubitem{(built-in exception)} |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 113 | |
| 114 | The following exceptions are the exceptions that are actually raised. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 115 | |
| 116 | \begin{excdesc}{AssertionError} |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 117 | \stindex{assert} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 118 | Raised when an \keyword{assert} statement fails. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 119 | \end{excdesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 120 | |
| 121 | \begin{excdesc}{AttributeError} |
| 122 | % xref to attribute reference? |
| 123 | Raised when an attribute reference or assignment fails. (When an |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 124 | object does not support attribute references or attribute assignments |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 125 | at all, \exception{TypeError} is raised.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 126 | \end{excdesc} |
| 127 | |
| 128 | \begin{excdesc}{EOFError} |
| 129 | % XXXJH xrefs here |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 130 | Raised when one of the built-in functions (\function{input()} or |
| 131 | \function{raw_input()}) hits an end-of-file condition (\EOF{}) without |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 132 | reading any data. |
| 133 | % XXXJH xrefs here |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 134 | (N.B.: the \method{read()} and \method{readline()} methods of file |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 135 | objects return an empty string when they hit \EOF{}.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 136 | \end{excdesc} |
| 137 | |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 138 | \begin{excdesc}{FloatingPointError} |
Fred Drake | b44e753 | 1998-07-27 21:11:42 +0000 | [diff] [blame] | 139 | Raised when a floating point operation fails. This exception is |
| 140 | always defined, but can only be raised when Python is configured |
Fred Drake | ee775a1 | 2000-04-11 19:46:40 +0000 | [diff] [blame] | 141 | with the \longprogramopt{with-fpectl} option, or the |
Fred Drake | b44e753 | 1998-07-27 21:11:42 +0000 | [diff] [blame] | 142 | \constant{WANT_SIGFPE_HANDLER} symbol is defined in the |
| 143 | \file{config.h} file. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 144 | \end{excdesc} |
| 145 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 146 | \begin{excdesc}{IOError} |
| 147 | % XXXJH xrefs here |
Fred Drake | b44e753 | 1998-07-27 21:11:42 +0000 | [diff] [blame] | 148 | Raised when an I/O operation (such as a \keyword{print} statement, |
| 149 | the built-in \function{open()} function or a method of a file |
| 150 | object) fails for an I/O-related reason, e.g., ``file not found'' or |
| 151 | ``disk full''. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 152 | |
Fred Drake | 02e18b4 | 1999-01-05 21:42:18 +0000 | [diff] [blame] | 153 | This class is derived from \exception{EnvironmentError}. See the |
Fred Drake | b44e753 | 1998-07-27 21:11:42 +0000 | [diff] [blame] | 154 | discussion above for more information on exception instance |
| 155 | attributes. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 156 | \end{excdesc} |
| 157 | |
| 158 | \begin{excdesc}{ImportError} |
| 159 | % XXXJH xref to import statement? |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 160 | Raised when an \keyword{import} statement fails to find the module |
Fred Drake | f65e323 | 1998-11-25 20:55:03 +0000 | [diff] [blame] | 161 | definition or when a \code{from \textrm{\ldots} import} fails to find a |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 162 | name that is to be imported. |
| 163 | \end{excdesc} |
| 164 | |
| 165 | \begin{excdesc}{IndexError} |
| 166 | % XXXJH xref to sequences |
| 167 | Raised when a sequence subscript is out of range. (Slice indices are |
| 168 | silently truncated to fall in the allowed range; if an index is not a |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 169 | plain integer, \exception{TypeError} is raised.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 170 | \end{excdesc} |
| 171 | |
| 172 | \begin{excdesc}{KeyError} |
| 173 | % XXXJH xref to mapping objects? |
| 174 | Raised when a mapping (dictionary) key is not found in the set of |
| 175 | existing keys. |
| 176 | \end{excdesc} |
| 177 | |
| 178 | \begin{excdesc}{KeyboardInterrupt} |
| 179 | Raised when the user hits the interrupt key (normally |
Fred Drake | 2a1cc3e | 1998-04-28 13:38:54 +0000 | [diff] [blame] | 180 | \kbd{Control-C} or \kbd{DEL}). During execution, a check for |
| 181 | interrupts is made regularly. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 182 | % XXXJH xrefs here |
Fred Drake | 2a1cc3e | 1998-04-28 13:38:54 +0000 | [diff] [blame] | 183 | Interrupts typed when a built-in function \function{input()} or |
| 184 | \function{raw_input()}) is waiting for input also raise this |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 185 | exception. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 186 | \end{excdesc} |
| 187 | |
| 188 | \begin{excdesc}{MemoryError} |
| 189 | Raised when an operation runs out of memory but the situation may |
| 190 | still be rescued (by deleting some objects). The associated value is |
| 191 | a string indicating what kind of (internal) operation ran out of memory. |
| 192 | Note that because of the underlying memory management architecture |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 193 | (C's \cfunction{malloc()} function), the interpreter may not |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 194 | always be able to completely recover from this situation; it |
| 195 | nevertheless raises an exception so that a stack traceback can be |
| 196 | printed, in case a run-away program was the cause. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 197 | \end{excdesc} |
| 198 | |
| 199 | \begin{excdesc}{NameError} |
| 200 | Raised when a local or global name is not found. This applies only |
| 201 | to unqualified names. The associated value is the name that could |
| 202 | not be found. |
| 203 | \end{excdesc} |
| 204 | |
Barry Warsaw | 6d26f4b | 1998-12-01 19:48:04 +0000 | [diff] [blame] | 205 | \begin{excdesc}{NotImplementedError} |
Barry Warsaw | 6d26f4b | 1998-12-01 19:48:04 +0000 | [diff] [blame] | 206 | This exception is derived from \exception{RuntimeError}. In user |
| 207 | defined base classes, abstract methods should raise this exception |
| 208 | when they require derived classes to override the method. |
Fred Drake | d0bceee | 1999-02-02 18:00:40 +0000 | [diff] [blame] | 209 | \versionadded{1.5.2} |
Barry Warsaw | 6d26f4b | 1998-12-01 19:48:04 +0000 | [diff] [blame] | 210 | \end{excdesc} |
| 211 | |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 212 | \begin{excdesc}{OSError} |
| 213 | %xref for os module |
Fred Drake | c457ca7 | 1998-07-23 20:31:53 +0000 | [diff] [blame] | 214 | This class is derived from \exception{EnvironmentError} and is used |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 215 | primarily as the \refmodule{os} module's \code{os.error} exception. |
Fred Drake | 98be47e | 1999-02-01 16:17:40 +0000 | [diff] [blame] | 216 | See \exception{EnvironmentError} above for a description of the |
| 217 | possible associated values. |
Fred Drake | d0bceee | 1999-02-02 18:00:40 +0000 | [diff] [blame] | 218 | \versionadded{1.5.2} |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 219 | \end{excdesc} |
| 220 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 221 | \begin{excdesc}{OverflowError} |
| 222 | % XXXJH reference to long's and/or int's? |
| 223 | Raised when the result of an arithmetic operation is too large to be |
| 224 | represented. This cannot occur for long integers (which would rather |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 225 | raise \exception{MemoryError} than give up). Because of the lack of |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 226 | standardization of floating point exception handling in C, most |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 227 | floating point operations also aren't checked. For plain integers, |
| 228 | all operations that can overflow are checked except left shift, where |
| 229 | typical applications prefer to drop bits than raise an exception. |
| 230 | \end{excdesc} |
| 231 | |
| 232 | \begin{excdesc}{RuntimeError} |
| 233 | Raised when an error is detected that doesn't fall in any of the |
| 234 | other categories. The associated value is a string indicating what |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 235 | precisely went wrong. (This exception is mostly a relic from a |
| 236 | previous version of the interpreter; it is not used very much any |
| 237 | more.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 238 | \end{excdesc} |
| 239 | |
| 240 | \begin{excdesc}{SyntaxError} |
| 241 | % XXXJH xref to these functions? |
| 242 | Raised when the parser encounters a syntax error. This may occur in |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 243 | an \keyword{import} statement, in an \keyword{exec} statement, in a call |
| 244 | to the built-in function \function{eval()} or \function{input()}, or |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 245 | when reading the initial script or standard input (also |
| 246 | interactively). |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 247 | |
| 248 | When class exceptions are used, instances of this class have |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 249 | atttributes \member{filename}, \member{lineno}, \member{offset} and |
| 250 | \member{text} for easier access to the details; for string exceptions, |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 251 | the associated value is usually a tuple of the form |
| 252 | \code{(message, (filename, lineno, offset, text))}. |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 253 | For class exceptions, \function{str()} returns only the message. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 254 | \end{excdesc} |
| 255 | |
| 256 | \begin{excdesc}{SystemError} |
| 257 | Raised when the interpreter finds an internal error, but the |
| 258 | situation does not look so serious to cause it to abandon all hope. |
| 259 | The associated value is a string indicating what went wrong (in |
| 260 | low-level terms). |
| 261 | |
| 262 | You should report this to the author or maintainer of your Python |
| 263 | interpreter. Be sure to report the version string of the Python |
| 264 | interpreter (\code{sys.version}; it is also printed at the start of an |
| 265 | interactive Python session), the exact error message (the exception's |
| 266 | associated value) and if possible the source of the program that |
| 267 | triggered the error. |
| 268 | \end{excdesc} |
| 269 | |
| 270 | \begin{excdesc}{SystemExit} |
| 271 | % XXXJH xref to module sys? |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 272 | This exception is raised by the \function{sys.exit()} function. When it |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 273 | is not handled, the Python interpreter exits; no stack traceback is |
| 274 | printed. If the associated value is a plain integer, it specifies the |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 275 | system exit status (passed to C's \cfunction{exit()} function); if it is |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 276 | \code{None}, the exit status is zero; if it has another type (such as |
| 277 | 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] | 278 | |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 279 | Instances have an attribute \member{code} which is set to the |
| 280 | proposed exit status or error message (defaulting to \code{None}). |
| 281 | Also, this exception derives directly from \exception{Exception} and |
| 282 | not \exception{StandardError}, since it is not technically an error. |
| 283 | |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 284 | A call to \function{sys.exit()} is translated into an exception so that |
| 285 | clean-up handlers (\keyword{finally} clauses of \keyword{try} statements) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 286 | can be executed, and so that a debugger can execute a script without |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 287 | running the risk of losing control. The \function{os._exit()} function |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 288 | can be used if it is absolutely positively necessary to exit |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 289 | immediately (e.g., after a \function{fork()} in the child process). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 290 | \end{excdesc} |
| 291 | |
| 292 | \begin{excdesc}{TypeError} |
| 293 | Raised when a built-in operation or function is applied to an object |
| 294 | of inappropriate type. The associated value is a string giving |
| 295 | details about the type mismatch. |
| 296 | \end{excdesc} |
| 297 | |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 298 | \begin{excdesc}{UnboundLocalError} |
| 299 | Raised when a reference is made to a local variable in a function or |
| 300 | method, but no value has been bound to that variable. This is a |
| 301 | subclass of \exception{NameError}. |
Fred Drake | 30f76ff | 2000-06-30 16:06:19 +0000 | [diff] [blame] | 302 | \versionadded{2.0} |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 303 | \end{excdesc} |
| 304 | |
Fred Drake | 3cb793e | 2000-04-06 14:48:35 +0000 | [diff] [blame] | 305 | \begin{excdesc}{UnicodeError} |
| 306 | Raised when a Unicode-related encoding or decoding error occurs. It |
| 307 | is a subclass of \exception{ValueError}. |
Fred Drake | 30f76ff | 2000-06-30 16:06:19 +0000 | [diff] [blame] | 308 | \versionadded{2.0} |
Fred Drake | 3cb793e | 2000-04-06 14:48:35 +0000 | [diff] [blame] | 309 | \end{excdesc} |
| 310 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 311 | \begin{excdesc}{ValueError} |
| 312 | Raised when a built-in operation or function receives an argument |
| 313 | that has the right type but an inappropriate value, and the |
| 314 | situation is not described by a more precise exception such as |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 315 | \exception{IndexError}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 316 | \end{excdesc} |
| 317 | |
Fred Drake | cebda6f | 2000-04-17 17:42:00 +0000 | [diff] [blame] | 318 | \begin{excdesc}{WindowsError} |
| 319 | Raised when a Windows-specific error occurs or when the error number |
| 320 | does not correspond to an \cdata{errno} value. The |
| 321 | \member{errno} and \member{strerror} values are created from the |
| 322 | return values of the \cfunction{GetLastError()} and |
| 323 | \cfunction{FormatMessage()} functions from the Windows Platform API. |
| 324 | This is a subclass of \exception{OSError}. |
Fred Drake | 30f76ff | 2000-06-30 16:06:19 +0000 | [diff] [blame] | 325 | \versionadded{2.0} |
Fred Drake | cebda6f | 2000-04-17 17:42:00 +0000 | [diff] [blame] | 326 | \end{excdesc} |
| 327 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 328 | \begin{excdesc}{ZeroDivisionError} |
| 329 | Raised when the second argument of a division or modulo operation is |
| 330 | zero. The associated value is a string indicating the type of the |
| 331 | operands and the operation. |
| 332 | \end{excdesc} |
Guido van Rossum | 1367b83 | 2000-12-19 04:27:54 +0000 | [diff] [blame] | 333 | |
| 334 | |
| 335 | \setindexsubitem{(built-in warning category)} |
| 336 | |
| 337 | The following exceptions are used as warning categories; see the |
| 338 | \module{warnings} module for more information. |
| 339 | |
| 340 | \begin{excdesc}{Warning} |
| 341 | Base class for warning categories. |
| 342 | \end{excdesc} |
| 343 | |
| 344 | \begin{excdesc}{UserWarning} |
| 345 | Base class for warnings generated by user code. |
| 346 | \end{excdesc} |
| 347 | |
| 348 | \begin{excdesc}{DeprecationWarning} |
| 349 | Base class for warnings about deprecated features. |
| 350 | \end{excdesc} |
| 351 | |
| 352 | \begin{excdesc}{SyntaxWarning} |
| 353 | Base class for warnings about dubious syntax |
| 354 | \end{excdesc} |
| 355 | |
| 356 | \begin{excdesc}{RuntimeWarning} |
| 357 | Base class for warnings about dubious runtime behavior. |
| 358 | \end{excdesc} |