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.

Frequently asked questions

How does Python handle common error?
This task is covered in 2 stacks on this page: Python, Regular Expressions. The "Common Built-in Exceptions" snippet in Python uses `ValueError, TypeError, KeyError, IndexError, ...`.
Which code does the Python example use?
The "Common Built-in Exceptions" snippet uses `ValueError, TypeError, KeyError, IndexError, ...`, from the Error Handling section of the Python cheat sheet.
Which stacks cover "common error" on this page?
Python, Regular Expressions. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Common Built-in Exceptions": Learn the hierarchy: FileNotFoundError is a subclass of OSError. Catching OSError also catches FileNotFoundError, PermissionError, etc.