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.

MethodSyntaxDescription
ValueErrorint("abc")Raised when a function receives an argument of correct type but inappropriate value.
TypeError"1" + 2Raised when an operation or function is applied to an object of inappropriate type.
IndexErrormy_list[10]Raised when a sequence subscript is out of range.
KeyErrormy_dict["missing"]Raised when a dictionary key is not found.
AttributeErrormy_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.

MethodSyntaxDescription
FileNotFoundErroropen("missing.txt")Raised when a file or directory is requested but does not exist.
ZeroDivisionError1 / 0Raised when the second argument of a division or modulo operation is zero.
SyntaxErrorwhile True print("hi")Raised when the parser encounters a syntax error.
NameErrorprint(undefined_variable)Raised when a local or global name is not found.
StopIterationnext(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.