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} |
Andrew M. Kuchling | 032bd0a | 2003-05-13 14:13:58 +0000 | [diff] [blame] | 4 | \modulesynopsis{Standard exception 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 | |
Neal Norwitz | 847207a | 2003-05-29 02:17:23 +0000 | [diff] [blame] | 7 | Exceptions should be class objects. |
Fred Drake | 7acb218 | 2000-09-09 03:28:00 +0000 | [diff] [blame] | 8 | The exceptions are defined in the module \module{exceptions}. This |
| 9 | module never needs to be imported explicitly: the exceptions are |
Fred Drake | c692055 | 2001-09-21 21:12:30 +0000 | [diff] [blame] | 10 | provided in the built-in namespace as well as the \module{exceptions} |
| 11 | module. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 12 | |
Neal Norwitz | 847207a | 2003-05-29 02:17:23 +0000 | [diff] [blame] | 13 | \begin{notice} |
| 14 | In past versions of Python string exceptions were supported. In |
| 15 | Python 1.5 and newer versions, all standard exceptions have been |
| 16 | converted to class objects and users are encouraged to do the same. |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 17 | String exceptions will raise a \code{DeprecationWarning} in Python 2.5 and |
| 18 | newer. |
Neal Norwitz | 847207a | 2003-05-29 02:17:23 +0000 | [diff] [blame] | 19 | In future versions, support for string exceptions will be removed. |
| 20 | |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 21 | Two distinct string objects with the same value are considered different |
| 22 | exceptions. This is done to force programmers to use exception names |
| 23 | rather than their string value when specifying exception handlers. |
| 24 | The string value of all built-in exceptions is their name, but this is |
| 25 | not a requirement for user-defined exceptions or exceptions defined by |
| 26 | library modules. |
Neal Norwitz | 847207a | 2003-05-29 02:17:23 +0000 | [diff] [blame] | 27 | \end{notice} |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 28 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 29 | For class exceptions, in a \keyword{try}\stindex{try} statement with |
| 30 | an \keyword{except}\stindex{except} clause that mentions a particular |
| 31 | class, that clause also handles any exception classes derived from |
| 32 | that class (but not exception classes from which \emph{it} is |
| 33 | derived). Two exception classes that are not related via subclassing |
| 34 | are never equivalent, even if they have the same name. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 35 | |
| 36 | The built-in exceptions listed below can be generated by the |
| 37 | interpreter or built-in functions. Except where mentioned, they have |
| 38 | an ``associated value'' indicating the detailed cause of the error. |
| 39 | This may be a string or a tuple containing several items of |
| 40 | information (e.g., an error code and a string explaining the code). |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 41 | The associated value is the second argument to the |
| 42 | \keyword{raise}\stindex{raise} statement. For string exceptions, the |
| 43 | associated value itself will be stored in the variable named as the |
| 44 | second argument of the \keyword{except} clause (if any). For class |
| 45 | exceptions, that variable receives the exception instance. If the |
| 46 | exception class is derived from the standard root class |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 47 | \exception{BaseException}, the associated value is present as the |
| 48 | exception instance's \member{args} attribute. If there is a single argument |
| 49 | (as is preferred), it is bound to the \member{message} attribute. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 50 | |
| 51 | 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] | 52 | exception handler or to report an error condition ``just like'' the |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 53 | situation in which the interpreter raises the same exception; but |
| 54 | beware that there is nothing to prevent user code from raising an |
| 55 | inappropriate error. |
| 56 | |
Fred Drake | c692055 | 2001-09-21 21:12:30 +0000 | [diff] [blame] | 57 | The built-in exception classes can be sub-classed to define new |
| 58 | exceptions; programmers are encouraged to at least derive new |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 59 | exceptions from the \exception{Exception} class and not |
| 60 | \exception{BaseException}. More |
Fred Drake | c692055 | 2001-09-21 21:12:30 +0000 | [diff] [blame] | 61 | information on defining exceptions is available in the |
| 62 | \citetitle[../tut/tut.html]{Python Tutorial} under the heading |
| 63 | ``User-defined Exceptions.'' |
| 64 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 65 | \setindexsubitem{(built-in exception base class)} |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 66 | |
| 67 | The following exceptions are only used as base classes for other |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 68 | exceptions. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 69 | |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 70 | \begin{excdesc}{BaseException} |
| 71 | The base class for all built-in exceptions. It is not meant to be directly |
| 72 | inherited by user-defined classes (for that use \exception{Exception}). If |
| 73 | \function{str()} or \function{unicode()} is called on an instance of this |
| 74 | class, the representation of the argument(s) to the instance are returned or |
| 75 | the emptry string when there were no arguments. If only a single argument is |
| 76 | passed in, it is stored in the \member{message} attribute. If more than one |
| 77 | argument is passed in, \member{message} is set to the empty string. These |
| 78 | semantics are meant to reflect the fact that \member{message} is to store a |
| 79 | text message explaining why the exception had been raised. If more data needs |
| 80 | to be attached to the exception, attach it through arbitrary attributes on the |
| 81 | instance. All arguments are also stored in \member{args} as a tuple, but it will |
| 82 | eventually be deprecated and thus its use is discouraged. |
| 83 | \versionchanged[Changed to inherit from \exception{BaseException}]{2.5} |
| 84 | \versionadded{2.5} |
| 85 | |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 86 | \begin{excdesc}{Exception} |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 87 | All built-in, non-system-exiting exceptions are derived |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 88 | from this class. All user-defined exceptions should also be derived |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 89 | from this class. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 90 | \end{excdesc} |
| 91 | |
| 92 | \begin{excdesc}{StandardError} |
Barry Warsaw | f2b4554 | 1999-02-24 00:27:14 +0000 | [diff] [blame] | 93 | The base class for all built-in exceptions except |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 94 | \exception{StopIteration}, \exception{GeneratorExit}, |
| 95 | \exception{KeyboardInterrupt} and \exception{SystemExit}. |
| 96 | \exception{StandardError} itself is derived from \exception{Exception}. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 97 | \end{excdesc} |
| 98 | |
| 99 | \begin{excdesc}{ArithmeticError} |
| 100 | The base class for those built-in exceptions that are raised for |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 101 | various arithmetic errors: \exception{OverflowError}, |
| 102 | \exception{ZeroDivisionError}, \exception{FloatingPointError}. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 103 | \end{excdesc} |
| 104 | |
| 105 | \begin{excdesc}{LookupError} |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 106 | 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] | 107 | index used on a mapping or sequence is invalid: \exception{IndexError}, |
Fred Drake | 53143be | 2000-10-25 21:05:29 +0000 | [diff] [blame] | 108 | \exception{KeyError}. This can be raised directly by |
| 109 | \function{sys.setdefaultencoding()}. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 110 | \end{excdesc} |
| 111 | |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 112 | \begin{excdesc}{EnvironmentError} |
| 113 | The base class for exceptions that |
| 114 | can occur outside the Python system: \exception{IOError}, |
| 115 | \exception{OSError}. When exceptions of this type are created with a |
| 116 | 2-tuple, the first item is available on the instance's \member{errno} |
| 117 | attribute (it is assumed to be an error number), and the second item |
| 118 | is available on the \member{strerror} attribute (it is usually the |
| 119 | associated error message). The tuple itself is also available on the |
| 120 | \member{args} attribute. |
Fred Drake | d0bceee | 1999-02-02 18:00:40 +0000 | [diff] [blame] | 121 | \versionadded{1.5.2} |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 122 | |
| 123 | When an \exception{EnvironmentError} exception is instantiated with a |
| 124 | 3-tuple, the first two items are available as above, while the third |
| 125 | item is available on the \member{filename} attribute. However, for |
| 126 | backwards compatibility, the \member{args} attribute contains only a |
| 127 | 2-tuple of the first two constructor arguments. |
| 128 | |
| 129 | The \member{filename} attribute is \code{None} when this exception is |
| 130 | created with other than 3 arguments. The \member{errno} and |
| 131 | \member{strerror} attributes are also \code{None} when the instance was |
| 132 | created with other than 2 or 3 arguments. In this last case, |
| 133 | \member{args} contains the verbatim constructor arguments as a tuple. |
| 134 | \end{excdesc} |
| 135 | |
Fred Drake | 88c023b | 2000-09-07 16:33:32 +0000 | [diff] [blame] | 136 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 137 | \setindexsubitem{(built-in exception)} |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 138 | |
| 139 | The following exceptions are the exceptions that are actually raised. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 140 | |
| 141 | \begin{excdesc}{AssertionError} |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 142 | \stindex{assert} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 143 | Raised when an \keyword{assert} statement fails. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 144 | \end{excdesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 145 | |
| 146 | \begin{excdesc}{AttributeError} |
| 147 | % xref to attribute reference? |
| 148 | Raised when an attribute reference or assignment fails. (When an |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 149 | object does not support attribute references or attribute assignments |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 150 | at all, \exception{TypeError} is raised.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 151 | \end{excdesc} |
| 152 | |
| 153 | \begin{excdesc}{EOFError} |
| 154 | % XXXJH xrefs here |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 155 | Raised when one of the built-in functions (\function{input()} or |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 156 | \function{raw_input()}) hits an end-of-file condition (\EOF) without |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 157 | reading any data. |
| 158 | % XXXJH xrefs here |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 159 | (N.B.: the \method{read()} and \method{readline()} methods of file |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 160 | objects return an empty string when they hit \EOF.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 161 | \end{excdesc} |
| 162 | |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 163 | \begin{excdesc}{FloatingPointError} |
Fred Drake | b44e753 | 1998-07-27 21:11:42 +0000 | [diff] [blame] | 164 | Raised when a floating point operation fails. This exception is |
| 165 | always defined, but can only be raised when Python is configured |
Fred Drake | ee775a1 | 2000-04-11 19:46:40 +0000 | [diff] [blame] | 166 | with the \longprogramopt{with-fpectl} option, or the |
Fred Drake | b44e753 | 1998-07-27 21:11:42 +0000 | [diff] [blame] | 167 | \constant{WANT_SIGFPE_HANDLER} symbol is defined in the |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 168 | \file{pyconfig.h} file. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 169 | \end{excdesc} |
| 170 | |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 171 | \begin{excdesv}{GeneratorExit} |
| 172 | Raise when a generator's \method{close()} method is called. |
| 173 | It directly inherits from \exception{Exception} instead of |
| 174 | \exception{StandardError} since it is technically not an error. |
| 175 | \versionadded{2.5} |
| 176 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 177 | \begin{excdesc}{IOError} |
| 178 | % XXXJH xrefs here |
Fred Drake | b44e753 | 1998-07-27 21:11:42 +0000 | [diff] [blame] | 179 | Raised when an I/O operation (such as a \keyword{print} statement, |
| 180 | the built-in \function{open()} function or a method of a file |
| 181 | object) fails for an I/O-related reason, e.g., ``file not found'' or |
| 182 | ``disk full''. |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 183 | |
Fred Drake | 02e18b4 | 1999-01-05 21:42:18 +0000 | [diff] [blame] | 184 | This class is derived from \exception{EnvironmentError}. See the |
Fred Drake | b44e753 | 1998-07-27 21:11:42 +0000 | [diff] [blame] | 185 | discussion above for more information on exception instance |
| 186 | attributes. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 187 | \end{excdesc} |
| 188 | |
| 189 | \begin{excdesc}{ImportError} |
| 190 | % XXXJH xref to import statement? |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 191 | Raised when an \keyword{import} statement fails to find the module |
Fred Drake | f65e323 | 1998-11-25 20:55:03 +0000 | [diff] [blame] | 192 | 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] | 193 | name that is to be imported. |
| 194 | \end{excdesc} |
| 195 | |
| 196 | \begin{excdesc}{IndexError} |
| 197 | % XXXJH xref to sequences |
| 198 | Raised when a sequence subscript is out of range. (Slice indices are |
| 199 | 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] | 200 | plain integer, \exception{TypeError} is raised.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 201 | \end{excdesc} |
| 202 | |
| 203 | \begin{excdesc}{KeyError} |
| 204 | % XXXJH xref to mapping objects? |
| 205 | Raised when a mapping (dictionary) key is not found in the set of |
| 206 | existing keys. |
| 207 | \end{excdesc} |
| 208 | |
| 209 | \begin{excdesc}{KeyboardInterrupt} |
| 210 | Raised when the user hits the interrupt key (normally |
Fred Drake | 682d5f3 | 2001-07-12 02:09:51 +0000 | [diff] [blame] | 211 | \kbd{Control-C} or \kbd{Delete}). During execution, a check for |
Fred Drake | 2a1cc3e | 1998-04-28 13:38:54 +0000 | [diff] [blame] | 212 | interrupts is made regularly. |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 213 | % XXX(hylton) xrefs here |
Fred Drake | 2a1cc3e | 1998-04-28 13:38:54 +0000 | [diff] [blame] | 214 | Interrupts typed when a built-in function \function{input()} or |
Raymond Hettinger | b4c1d9b | 2003-05-10 08:51:28 +0000 | [diff] [blame] | 215 | \function{raw_input()} is waiting for input also raise this |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 216 | exception. |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 217 | The exception inherits from \exception{BaseException} so as to not be |
| 218 | accidentally caught by code that catches \exception{Exception} and thus |
| 219 | prevent the interpreter from exiting. |
| 220 | \versionchanged[Changed to inherit from \exception{BaseException}]{2.5} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 221 | \end{excdesc} |
| 222 | |
| 223 | \begin{excdesc}{MemoryError} |
| 224 | Raised when an operation runs out of memory but the situation may |
| 225 | still be rescued (by deleting some objects). The associated value is |
| 226 | a string indicating what kind of (internal) operation ran out of memory. |
| 227 | Note that because of the underlying memory management architecture |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 228 | (C's \cfunction{malloc()} function), the interpreter may not |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 229 | always be able to completely recover from this situation; it |
| 230 | nevertheless raises an exception so that a stack traceback can be |
| 231 | printed, in case a run-away program was the cause. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 232 | \end{excdesc} |
| 233 | |
| 234 | \begin{excdesc}{NameError} |
| 235 | Raised when a local or global name is not found. This applies only |
Raymond Hettinger | 9240be2 | 2002-08-27 23:53:23 +0000 | [diff] [blame] | 236 | to unqualified names. The associated value is an error message that |
| 237 | includes the name that could not be found. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 238 | \end{excdesc} |
| 239 | |
Barry Warsaw | 6d26f4b | 1998-12-01 19:48:04 +0000 | [diff] [blame] | 240 | \begin{excdesc}{NotImplementedError} |
Barry Warsaw | 6d26f4b | 1998-12-01 19:48:04 +0000 | [diff] [blame] | 241 | This exception is derived from \exception{RuntimeError}. In user |
| 242 | defined base classes, abstract methods should raise this exception |
| 243 | when they require derived classes to override the method. |
Fred Drake | d0bceee | 1999-02-02 18:00:40 +0000 | [diff] [blame] | 244 | \versionadded{1.5.2} |
Barry Warsaw | 6d26f4b | 1998-12-01 19:48:04 +0000 | [diff] [blame] | 245 | \end{excdesc} |
| 246 | |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 247 | \begin{excdesc}{OSError} |
| 248 | %xref for os module |
Fred Drake | c457ca7 | 1998-07-23 20:31:53 +0000 | [diff] [blame] | 249 | This class is derived from \exception{EnvironmentError} and is used |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 250 | primarily as the \refmodule{os} module's \code{os.error} exception. |
Fred Drake | 98be47e | 1999-02-01 16:17:40 +0000 | [diff] [blame] | 251 | See \exception{EnvironmentError} above for a description of the |
| 252 | possible associated values. |
Fred Drake | d0bceee | 1999-02-02 18:00:40 +0000 | [diff] [blame] | 253 | \versionadded{1.5.2} |
Barry Warsaw | da00c87 | 1998-07-23 19:57:35 +0000 | [diff] [blame] | 254 | \end{excdesc} |
| 255 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 256 | \begin{excdesc}{OverflowError} |
| 257 | % XXXJH reference to long's and/or int's? |
| 258 | Raised when the result of an arithmetic operation is too large to be |
| 259 | represented. This cannot occur for long integers (which would rather |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 260 | raise \exception{MemoryError} than give up). Because of the lack of |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 261 | standardization of floating point exception handling in C, most |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 262 | floating point operations also aren't checked. For plain integers, |
| 263 | all operations that can overflow are checked except left shift, where |
| 264 | typical applications prefer to drop bits than raise an exception. |
| 265 | \end{excdesc} |
| 266 | |
Fred Drake | 8c2c3d3 | 2001-10-06 06:10:54 +0000 | [diff] [blame] | 267 | \begin{excdesc}{ReferenceError} |
| 268 | This exception is raised when a weak reference proxy, created by the |
| 269 | \function{\refmodule{weakref}.proxy()} function, is used to access |
| 270 | an attribute of the referent after it has been garbage collected. |
| 271 | For more information on weak references, see the \refmodule{weakref} |
| 272 | module. |
| 273 | \versionadded[Previously known as the |
| 274 | \exception{\refmodule{weakref}.ReferenceError} |
| 275 | exception]{2.2} |
| 276 | \end{excdesc} |
| 277 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 278 | \begin{excdesc}{RuntimeError} |
| 279 | Raised when an error is detected that doesn't fall in any of the |
| 280 | other categories. The associated value is a string indicating what |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 281 | precisely went wrong. (This exception is mostly a relic from a |
| 282 | previous version of the interpreter; it is not used very much any |
| 283 | more.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 284 | \end{excdesc} |
| 285 | |
Fred Drake | 9cfe182 | 2001-05-03 04:30:45 +0000 | [diff] [blame] | 286 | \begin{excdesc}{StopIteration} |
| 287 | Raised by an iterator's \method{next()} method to signal that there |
| 288 | are no further values. |
| 289 | This is derived from \exception{Exception} rather than |
| 290 | \exception{StandardError}, since this is not considered an error in |
| 291 | its normal application. |
Fred Drake | f42cc45 | 2001-05-03 04:39:10 +0000 | [diff] [blame] | 292 | \versionadded{2.2} |
Fred Drake | 9cfe182 | 2001-05-03 04:30:45 +0000 | [diff] [blame] | 293 | \end{excdesc} |
| 294 | |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 295 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 296 | \begin{excdesc}{SyntaxError} |
| 297 | % XXXJH xref to these functions? |
| 298 | Raised when the parser encounters a syntax error. This may occur in |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 299 | an \keyword{import} statement, in an \keyword{exec} statement, in a call |
| 300 | to the built-in function \function{eval()} or \function{input()}, or |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 301 | when reading the initial script or standard input (also |
| 302 | interactively). |
Guido van Rossum | df3dba0 | 1997-10-05 18:51:26 +0000 | [diff] [blame] | 303 | |
Raymond Hettinger | 6880431 | 2005-01-01 00:28:46 +0000 | [diff] [blame] | 304 | Instances of this class have attributes \member{filename}, |
Fred Drake | c692055 | 2001-09-21 21:12:30 +0000 | [diff] [blame] | 305 | \member{lineno}, \member{offset} and \member{text} for easier access |
| 306 | to the details. \function{str()} of the exception instance returns |
| 307 | only the message. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 308 | \end{excdesc} |
| 309 | |
| 310 | \begin{excdesc}{SystemError} |
| 311 | Raised when the interpreter finds an internal error, but the |
| 312 | situation does not look so serious to cause it to abandon all hope. |
| 313 | The associated value is a string indicating what went wrong (in |
| 314 | low-level terms). |
| 315 | |
| 316 | You should report this to the author or maintainer of your Python |
Fred Drake | c692055 | 2001-09-21 21:12:30 +0000 | [diff] [blame] | 317 | interpreter. Be sure to report the version of the Python |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 318 | interpreter (\code{sys.version}; it is also printed at the start of an |
| 319 | interactive Python session), the exact error message (the exception's |
| 320 | associated value) and if possible the source of the program that |
| 321 | triggered the error. |
| 322 | \end{excdesc} |
| 323 | |
| 324 | \begin{excdesc}{SystemExit} |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 325 | % XXX(hylton) xref to module sys? |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 326 | 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] | 327 | is not handled, the Python interpreter exits; no stack traceback is |
| 328 | printed. If the associated value is a plain integer, it specifies the |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 329 | 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] | 330 | \code{None}, the exit status is zero; if it has another type (such as |
| 331 | 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] | 332 | |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 333 | Instances have an attribute \member{code} which is set to the |
| 334 | proposed exit status or error message (defaulting to \code{None}). |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 335 | Also, this exception derives directly from \exception{BaseException} and |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 336 | not \exception{StandardError}, since it is not technically an error. |
| 337 | |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 338 | A call to \function{sys.exit()} is translated into an exception so that |
| 339 | clean-up handlers (\keyword{finally} clauses of \keyword{try} statements) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 340 | 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] | 341 | running the risk of losing control. The \function{os._exit()} function |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 342 | can be used if it is absolutely positively necessary to exit |
Fred Drake | c046e97 | 2001-07-23 19:19:39 +0000 | [diff] [blame] | 343 | immediately (for example, in the child process after a call to |
| 344 | \function{fork()}). |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 345 | |
| 346 | The exception inherits from \exception{BaseException} instead of |
| 347 | \exception{StandardError} or \exception{Exception} so that it is not |
| 348 | accidentally caught by code that catches \exception{Exception}. This allows |
| 349 | the exception to properly propagate up and cause the interpreter to exit. |
| 350 | \versionchanged[Changed to inherit from \exception{BaseException}]{2.5} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 351 | \end{excdesc} |
| 352 | |
| 353 | \begin{excdesc}{TypeError} |
Raymond Hettinger | 4ee2ff3 | 2003-08-04 08:33:50 +0000 | [diff] [blame] | 354 | Raised when an operation or function is applied to an object |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 355 | of inappropriate type. The associated value is a string giving |
| 356 | details about the type mismatch. |
| 357 | \end{excdesc} |
| 358 | |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 359 | \begin{excdesc}{UnboundLocalError} |
| 360 | Raised when a reference is made to a local variable in a function or |
| 361 | method, but no value has been bound to that variable. This is a |
| 362 | subclass of \exception{NameError}. |
Fred Drake | 30f76ff | 2000-06-30 16:06:19 +0000 | [diff] [blame] | 363 | \versionadded{2.0} |
Fred Drake | 5828ad6 | 2000-04-06 15:03:01 +0000 | [diff] [blame] | 364 | \end{excdesc} |
| 365 | |
Fred Drake | 3cb793e | 2000-04-06 14:48:35 +0000 | [diff] [blame] | 366 | \begin{excdesc}{UnicodeError} |
| 367 | Raised when a Unicode-related encoding or decoding error occurs. It |
| 368 | is a subclass of \exception{ValueError}. |
Fred Drake | 30f76ff | 2000-06-30 16:06:19 +0000 | [diff] [blame] | 369 | \versionadded{2.0} |
Fred Drake | 3cb793e | 2000-04-06 14:48:35 +0000 | [diff] [blame] | 370 | \end{excdesc} |
| 371 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 372 | \begin{excdesc}{UnicodeEncodeError} |
| 373 | Raised when a Unicode-related error occurs during encoding. It |
| 374 | is a subclass of \exception{UnicodeError}. |
| 375 | \versionadded{2.3} |
| 376 | \end{excdesc} |
| 377 | |
| 378 | \begin{excdesc}{UnicodeDecodeError} |
| 379 | Raised when a Unicode-related error occurs during decoding. It |
| 380 | is a subclass of \exception{UnicodeError}. |
| 381 | \versionadded{2.3} |
| 382 | \end{excdesc} |
| 383 | |
| 384 | \begin{excdesc}{UnicodeTranslateError} |
| 385 | Raised when a Unicode-related error occurs during translating. It |
| 386 | is a subclass of \exception{UnicodeError}. |
| 387 | \versionadded{2.3} |
| 388 | \end{excdesc} |
| 389 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 390 | \begin{excdesc}{ValueError} |
| 391 | Raised when a built-in operation or function receives an argument |
| 392 | that has the right type but an inappropriate value, and the |
| 393 | situation is not described by a more precise exception such as |
Fred Drake | 27467e4 | 1998-07-23 19:47:41 +0000 | [diff] [blame] | 394 | \exception{IndexError}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 395 | \end{excdesc} |
| 396 | |
Fred Drake | cebda6f | 2000-04-17 17:42:00 +0000 | [diff] [blame] | 397 | \begin{excdesc}{WindowsError} |
| 398 | Raised when a Windows-specific error occurs or when the error number |
| 399 | does not correspond to an \cdata{errno} value. The |
| 400 | \member{errno} and \member{strerror} values are created from the |
| 401 | return values of the \cfunction{GetLastError()} and |
| 402 | \cfunction{FormatMessage()} functions from the Windows Platform API. |
| 403 | This is a subclass of \exception{OSError}. |
Fred Drake | 30f76ff | 2000-06-30 16:06:19 +0000 | [diff] [blame] | 404 | \versionadded{2.0} |
Fred Drake | cebda6f | 2000-04-17 17:42:00 +0000 | [diff] [blame] | 405 | \end{excdesc} |
| 406 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 407 | \begin{excdesc}{ZeroDivisionError} |
| 408 | Raised when the second argument of a division or modulo operation is |
| 409 | zero. The associated value is a string indicating the type of the |
| 410 | operands and the operation. |
| 411 | \end{excdesc} |
Guido van Rossum | 1367b83 | 2000-12-19 04:27:54 +0000 | [diff] [blame] | 412 | |
| 413 | |
Fred Drake | c692055 | 2001-09-21 21:12:30 +0000 | [diff] [blame] | 414 | \setindexsubitem{(built-in warning)} |
Guido van Rossum | 1367b83 | 2000-12-19 04:27:54 +0000 | [diff] [blame] | 415 | |
| 416 | The following exceptions are used as warning categories; see the |
Barry Warsaw | b8c20a7 | 2002-08-14 16:40:54 +0000 | [diff] [blame] | 417 | \refmodule{warnings} module for more information. |
Guido van Rossum | 1367b83 | 2000-12-19 04:27:54 +0000 | [diff] [blame] | 418 | |
| 419 | \begin{excdesc}{Warning} |
| 420 | Base class for warning categories. |
| 421 | \end{excdesc} |
| 422 | |
| 423 | \begin{excdesc}{UserWarning} |
| 424 | Base class for warnings generated by user code. |
| 425 | \end{excdesc} |
| 426 | |
| 427 | \begin{excdesc}{DeprecationWarning} |
| 428 | Base class for warnings about deprecated features. |
| 429 | \end{excdesc} |
| 430 | |
Neal Norwitz | d68f517 | 2002-05-29 15:54:55 +0000 | [diff] [blame] | 431 | \begin{excdesc}{PendingDeprecationWarning} |
| 432 | Base class for warnings about features which will be deprecated in the future. |
| 433 | \end{excdesc} |
| 434 | |
Guido van Rossum | 1367b83 | 2000-12-19 04:27:54 +0000 | [diff] [blame] | 435 | \begin{excdesc}{SyntaxWarning} |
| 436 | Base class for warnings about dubious syntax |
| 437 | \end{excdesc} |
| 438 | |
| 439 | \begin{excdesc}{RuntimeWarning} |
| 440 | Base class for warnings about dubious runtime behavior. |
| 441 | \end{excdesc} |
Skip Montanaro | bb6bbc4 | 2002-03-28 20:53:22 +0000 | [diff] [blame] | 442 | |
Barry Warsaw | b8c20a7 | 2002-08-14 16:40:54 +0000 | [diff] [blame] | 443 | \begin{excdesc}{FutureWarning} |
| 444 | Base class for warnings about constructs that will change semantically |
| 445 | in the future. |
| 446 | \end{excdesc} |
| 447 | |
Fred Drake | 8d62e94 | 2002-03-28 21:06:17 +0000 | [diff] [blame] | 448 | The class hierarchy for built-in exceptions is: |
Skip Montanaro | bb6bbc4 | 2002-03-28 20:53:22 +0000 | [diff] [blame] | 449 | |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame^] | 450 | \verbatiminput{../../Lib/test/exception_hierarchy.txt} |