Common error

2 snippets across 2 stacks — Python, Regular Expressions

Also written as common errors

PYPython

Common Built-in Exceptions

PY · Error Handling
syntax
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 found

Note Learn the hierarchy: FileNotFoundError is a subclass of OSError. Catching OSError also catches FileNotFoundError, PermissionError, etc.

RXRegular Expressions

Forgetting to Escape the Dot

RX · Common Mistakes
syntax
. matches ANY character, \. matches a literal period
example
// Wrong: matches 'a.b' but also 'axb', 'a5b', etc.
/a.b/.test('axb')     // true (unintended!)

// Correct: matches only 'a.b'
/a\.b/.test('axb')    // false
/a\.b/.test('a.b')    // true
output
Unescaped dot matches too broadly

Note This is the most frequent regex beginner mistake. Domains, IP addresses, file extensions, and version numbers all contain literal dots that must be escaped. When reviewing regex, always check that dots are escaped where literal periods are intended.