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