Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Exceptions} |
| 2 | |
| 3 | Exceptions are string objects. Two distinct string objects with the |
| 4 | same value are different exceptions. This is done to force programmers |
| 5 | to use exception names rather than their string value when specifying |
| 6 | exception handlers. The string value of all built-in exceptions is |
| 7 | their name, but this is not a requirement for user-defined exceptions |
| 8 | or exceptions defined by library modules. |
| 9 | |
| 10 | The following exceptions can be generated by the interpreter or |
| 11 | built-in functions. Except where mentioned, they have an `associated |
| 12 | value' indicating the detailed cause of the error. This may be a |
| 13 | string or a tuple containing several items of information (e.g., an |
| 14 | error code and a string explaining the code). |
| 15 | |
| 16 | User code can raise built-in exceptions. This can be used to test an |
| 17 | exception handler or to report an error condition `just like' the |
| 18 | situation in which the interpreter raises the same exception; but |
| 19 | beware that there is nothing to prevent user code from raising an |
| 20 | inappropriate error. |
| 21 | |
| 22 | \renewcommand{\indexsubitem}{(built-in exception)} |
| 23 | |
| 24 | \begin{excdesc}{AttributeError} |
| 25 | % xref to attribute reference? |
| 26 | Raised when an attribute reference or assignment fails. (When an |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame^] | 27 | object does not support attribute references or attribute assignments |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 28 | at all, \code{TypeError} is raised.) |
| 29 | \end{excdesc} |
| 30 | |
| 31 | \begin{excdesc}{EOFError} |
| 32 | % XXXJH xrefs here |
| 33 | Raised when one of the built-in functions (\code{input()} or |
| 34 | \code{raw_input()}) hits an end-of-file condition (\EOF{}) without |
| 35 | reading any data. |
| 36 | % XXXJH xrefs here |
| 37 | (N.B.: the \code{read()} and \code{readline()} methods of file |
| 38 | objects return an empty string when they hit \EOF{}.) No associated value. |
| 39 | \end{excdesc} |
| 40 | |
| 41 | \begin{excdesc}{IOError} |
| 42 | % XXXJH xrefs here |
| 43 | Raised when an I/O operation (such as a \code{print} statement, the |
| 44 | built-in \code{open()} function or a method of a file object) fails |
| 45 | for an I/O-related reason, e.g., `file not found', `disk full'. |
| 46 | \end{excdesc} |
| 47 | |
| 48 | \begin{excdesc}{ImportError} |
| 49 | % XXXJH xref to import statement? |
| 50 | Raised when an \code{import} statement fails to find the module |
| 51 | definition or when a \code{from {\rm \ldots} import} fails to find a |
| 52 | name that is to be imported. |
| 53 | \end{excdesc} |
| 54 | |
| 55 | \begin{excdesc}{IndexError} |
| 56 | % XXXJH xref to sequences |
| 57 | Raised when a sequence subscript is out of range. (Slice indices are |
| 58 | silently truncated to fall in the allowed range; if an index is not a |
| 59 | plain integer, \code{TypeError} is raised.) |
| 60 | \end{excdesc} |
| 61 | |
| 62 | \begin{excdesc}{KeyError} |
| 63 | % XXXJH xref to mapping objects? |
| 64 | Raised when a mapping (dictionary) key is not found in the set of |
| 65 | existing keys. |
| 66 | \end{excdesc} |
| 67 | |
| 68 | \begin{excdesc}{KeyboardInterrupt} |
| 69 | Raised when the user hits the interrupt key (normally |
| 70 | \kbd{Control-C} or |
| 71 | \key{DEL}). During execution, a check for interrupts is made regularly. |
| 72 | % XXXJH xrefs here |
| 73 | Interrupts typed when a built-in function \code{input()} or |
| 74 | \code{raw_input()}) is waiting for input also raise this exception. No |
| 75 | associated value. |
| 76 | \end{excdesc} |
| 77 | |
| 78 | \begin{excdesc}{MemoryError} |
| 79 | Raised when an operation runs out of memory but the situation may |
| 80 | still be rescued (by deleting some objects). The associated value is |
| 81 | a string indicating what kind of (internal) operation ran out of memory. |
| 82 | Note that because of the underlying memory management architecture |
| 83 | (\C{}'s \code{malloc()} function), the interpreter may not always be able |
| 84 | to completely recover from this situation; it nevertheless raises an |
| 85 | exception so that a stack traceback can be printed, in case a run-away |
| 86 | program was the cause. |
| 87 | \end{excdesc} |
| 88 | |
| 89 | \begin{excdesc}{NameError} |
| 90 | Raised when a local or global name is not found. This applies only |
| 91 | to unqualified names. The associated value is the name that could |
| 92 | not be found. |
| 93 | \end{excdesc} |
| 94 | |
| 95 | \begin{excdesc}{OverflowError} |
| 96 | % XXXJH reference to long's and/or int's? |
| 97 | Raised when the result of an arithmetic operation is too large to be |
| 98 | represented. This cannot occur for long integers (which would rather |
| 99 | raise \code{MemoryError} than give up). Because of the lack of |
| 100 | standardization of floating point exception handling in \C{}, most |
| 101 | floating point operations also aren't checked. For plain integers, |
| 102 | all operations that can overflow are checked except left shift, where |
| 103 | typical applications prefer to drop bits than raise an exception. |
| 104 | \end{excdesc} |
| 105 | |
| 106 | \begin{excdesc}{RuntimeError} |
| 107 | Raised when an error is detected that doesn't fall in any of the |
| 108 | other categories. The associated value is a string indicating what |
| 109 | precisely went wrong. (This exception is a relic from a previous |
| 110 | version of the interpreter; it is not used any more except by some |
| 111 | extension modules that haven't been converted to define their own |
| 112 | exceptions yet.) |
| 113 | \end{excdesc} |
| 114 | |
| 115 | \begin{excdesc}{SyntaxError} |
| 116 | % XXXJH xref to these functions? |
| 117 | Raised when the parser encounters a syntax error. This may occur in |
| 118 | an \code{import} statement, in an \code{exec} statement, in a call |
| 119 | to the built-in function \code{eval()} or \code{input()}, or |
| 120 | when reading the initial script or standard input (also |
| 121 | interactively). |
| 122 | \end{excdesc} |
| 123 | |
| 124 | \begin{excdesc}{SystemError} |
| 125 | Raised when the interpreter finds an internal error, but the |
| 126 | situation does not look so serious to cause it to abandon all hope. |
| 127 | The associated value is a string indicating what went wrong (in |
| 128 | low-level terms). |
| 129 | |
| 130 | You should report this to the author or maintainer of your Python |
| 131 | interpreter. Be sure to report the version string of the Python |
| 132 | interpreter (\code{sys.version}; it is also printed at the start of an |
| 133 | interactive Python session), the exact error message (the exception's |
| 134 | associated value) and if possible the source of the program that |
| 135 | triggered the error. |
| 136 | \end{excdesc} |
| 137 | |
| 138 | \begin{excdesc}{SystemExit} |
| 139 | % XXXJH xref to module sys? |
| 140 | This exception is raised by the \code{sys.exit()} function. When it |
| 141 | is not handled, the Python interpreter exits; no stack traceback is |
| 142 | printed. If the associated value is a plain integer, it specifies the |
| 143 | system exit status (passed to \C{}'s \code{exit()} function); if it is |
| 144 | \code{None}, the exit status is zero; if it has another type (such as |
| 145 | a string), the object's value is printed and the exit status is one. |
| 146 | |
| 147 | A call to \code{sys.exit} is translated into an exception so that |
| 148 | clean-up handlers (\code{finally} clauses of \code{try} statements) |
| 149 | can be executed, and so that a debugger can execute a script without |
| 150 | running the risk of losing control. The \code{posix._exit()} function |
| 151 | can be used if it is absolutely positively necessary to exit |
| 152 | immediately (e.g., after a \code{fork()} in the child process). |
| 153 | \end{excdesc} |
| 154 | |
| 155 | \begin{excdesc}{TypeError} |
| 156 | Raised when a built-in operation or function is applied to an object |
| 157 | of inappropriate type. The associated value is a string giving |
| 158 | details about the type mismatch. |
| 159 | \end{excdesc} |
| 160 | |
| 161 | \begin{excdesc}{ValueError} |
| 162 | Raised when a built-in operation or function receives an argument |
| 163 | that has the right type but an inappropriate value, and the |
| 164 | situation is not described by a more precise exception such as |
| 165 | \code{IndexError}. |
| 166 | \end{excdesc} |
| 167 | |
| 168 | \begin{excdesc}{ZeroDivisionError} |
| 169 | Raised when the second argument of a division or modulo operation is |
| 170 | zero. The associated value is a string indicating the type of the |
| 171 | operands and the operation. |
| 172 | \end{excdesc} |