Appendix G — Error Types
G.1 Common Errors
There are many types of errors that the Python interpreter will recognize and the range of information provided in description strings is quite varied. A full list of the standard errors types in Python can be found here and are referred to as exceptions in Python. Those that you will commonly run into are summarized here:
Exception | Description | |
OverflowError
|
Raised when the result of an arithmetic operation is too large to be represented. This cannot occur for long integers (which would rather raise MemoryError than give up) and for most operations with plain integers, which return a long integer instead.
|
from math import * exp(1000) OverflowError: math range error
|
MemoryError
|
Raised when an operation runs out of memory but the situation may still be rescued (by deleting some objects). The associated value is a string indicating what kind of (internal) operation ran out of memory. | |
ZeroDivisionError
|
Raised when the second argument of a division or modulo operation is zero. The associated value is a string indicating the type of the operands and the operation. |
2 % 0 ZeroDivisionError: integer division or modulo by zero
|
FloatingPointError
|
Raised when a floating point operation fails. | |
IOError
|
Raised when an I/O operation (such as a print statement, the built-in open() function or a method of a file object) fails for an I/O-related reason, e.g., “file not found” or “disk full”.
|
open('some_file','r') IOError: [Errno 2] No such file or directory: 'some_file'
|
NameError
|
Raised when a local or global name is not found. The associated value is an error message that includes the name that could not be found. |
random_var NameError: name 'random_var' is not defined
|
SyntaxError
|
Raised when the parser__*__ encounters a syntax error. This may occur in an import statement, in an exec statement, in a call to the built-in function eval() or input(), or when reading the initial script or standard input (also interactively). |
1 = var SyntaxError: can't assign to literal
|
Indentation Error
|
Raised when the parser encounters syntax errors related to incorrect indentation. |
We will see this when we encounter functions, if -statements, and for and while loops later on.
|
TypeError
|
Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch. |
"str1" - "str2" TypeError: unsupported operand type(s) for -: 'str' and 'str'
|
ValueError
|
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception. |
float('hello') ValueError: could not convert string to float: hello
|
___*parser:___ a program that analyzes inputted code in accordance with the rules of a defined formal grammar (i.e. syntax).
G.2 Hierarchy of All Errors
The following hierarchy shows all of Python’s built-in errors, and how they relate to each other, such that ZeroDivisionError
is a type of ArithmeticError
.
BaseException
├── BaseExceptionGroup
├── GeneratorExit
├── KeyboardInterrupt
├── SystemExit
└── Exception
├── ArithmeticError
│ ├── FloatingPointError
│ ├── OverflowError
│ └── ZeroDivisionError
├── AssertionError
├── AttributeError
├── BufferError
├── EOFError
├── ExceptionGroup [BaseExceptionGroup]
├── ImportError
│ └── ModuleNotFoundError
├── LookupError
│ ├── IndexError
│ └── KeyError
├── MemoryError
├── NameError
│ └── UnboundLocalError
├── OSError
│ ├── BlockingIOError
│ ├── ChildProcessError
│ ├── ConnectionError
│ │ ├── BrokenPipeError
│ │ ├── ConnectionAbortedError
│ │ ├── ConnectionRefusedError
│ │ └── ConnectionResetError
│ ├── FileExistsError
│ ├── FileNotFoundError
│ ├── InterruptedError
│ ├── IsADirectoryError
│ ├── NotADirectoryError
│ ├── PermissionError
│ ├── ProcessLookupError
│ └── TimeoutError
├── ReferenceError
├── RuntimeError
│ ├── NotImplementedError
│ └── RecursionError
├── StopAsyncIteration
├── StopIteration
├── SyntaxError
│ └── IndentationError
│ └── TabError
├── SystemError
├── TypeError
├── ValueError
│ └── UnicodeError
│ ├── UnicodeDecodeError
│ ├── UnicodeEncodeError
│ └── UnicodeTranslateError
└── Warning
├── BytesWarning
├── DeprecationWarning
├── EncodingWarning
├── FutureWarning
├── ImportWarning
├── PendingDeprecationWarning
├── ResourceWarning
├── RuntimeWarning
├── SyntaxWarning
├── UnicodeWarning
└── UserWarning