Python Built-in Exception & Error Types Cheat Sheet
Reference guide for common Python error types. Learn how to debug ValueError, TypeError, IndexError, KeyError, and raise exceptions.
Common Logic & Value Exceptions
Exceptions representing mismatched operations, missing indices, or variable failures.
| Method | Syntax | Description |
|---|---|---|
| ValueError | int("abc") | Raised when a function receives an argument of correct type but inappropriate value. |
| TypeError | "1" + 2 | Raised when an operation or function is applied to an object of inappropriate type. |
| IndexError | my_list[10] | Raised when a sequence subscript is out of range. |
| KeyError | my_dict["missing"] | Raised when a dictionary key is not found. |
| AttributeError | my_str.append("x") | Raised when an attribute reference or assignment fails (e.g. calling list methods on string). |
Runtime & Resource Exceptions
Issues representing external file systems, calculation crashes, or execution syntax errors.
| Method | Syntax | Description |
|---|---|---|
| FileNotFoundError | open("missing.txt") | Raised when a file or directory is requested but does not exist. |
| ZeroDivisionError | 1 / 0 | Raised when the second argument of a division or modulo operation is zero. |
| SyntaxError | while True print("hi") | Raised when the parser encounters a syntax error. |
| NameError | print(undefined_variable) | Raised when a local or global name is not found. |
| StopIteration | next(exhausted_iterator) | Raised by built-in next() to signal that the iterator has no further items. |
Frequently Asked Questions
How do I catch multiple exceptions in Python?
You can specify them as a tuple in a single except block, e.g., except (ValueError, TypeError) as e:.
When should I raise exceptions manually?
Raise exceptions using the raise keyword when an unexpected condition occurs in your code that prevents normal execution, such as validation failures.
Keep Learning
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Try/Except & Error Handling
Prevent your Python scripts from crashing. Learn try, except, finally blocks and how to raise custom exceptions properly.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python vs TypeScript: Dynamic Scripting vs Type-Safe Web
Compare Python and TypeScript. Learn about structural type systems, decorator metadata, compiler checks, web runtimes, and developer tooling.