Common Built-in Exceptions
PY · Error Handlingsyntax
ValueError, TypeError, KeyError, IndexError, ...example
# ValueError - wrong value for the type
# TypeError - wrong type entirely
# KeyError - missing dictionary key
# IndexError - list index out of range
# AttributeError - missing attribute
# FileNotFoundError - file doesn't exist
# PermissionError - insufficient permissions
# StopIteration - iterator exhausted
try:
open("nonexistent.txt")
except FileNotFoundError:
print("File not found")output
File not foundNote Learn the hierarchy: FileNotFoundError is a subclass of OSError. Catching OSError also catches FileNotFoundError, PermissionError, etc.