Error types

2 snippets across 2 stacks — JavaScript, Python

JSJavaScript

Built-in Error Types

JS · Error Handling
syntax
TypeError | RangeError | ReferenceError | SyntaxError | URIError | EvalError | AggregateError
example
// TypeError - wrong type for an operation
try { null.toString(); }
catch (e) { console.log(e.constructor.name); } // "TypeError"

// RangeError - value out of allowed range
try { new Array(-1); }
catch (e) { console.log(e.constructor.name); } // "RangeError"

// ReferenceError - undeclared variable
try { console.log(undeclaredVar); }
catch (e) { console.log(e.constructor.name); } // "ReferenceError"

Note Catching specific types helps you handle known errors differently from unexpected ones. AggregateError wraps multiple errors (used by Promise.any()).

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.