Reading Exception Types
Python, JavaScript, and Java exception types — understanding what went wrong from the error name
Common exception types
- TypeError — wrong type for operation (JS: property on undefined; Python: 1 + "str")
- ValueError — right type, invalid value (percentage = 150; int("hello"))
- KeyError (Python) / ReferenceError (JS) — missing key or undefined variable
- NullPointerException (Java) — method called on a null reference
- ImportError (Python) — module or name not found
- IndexError — list/array index out of bounds
Question 0 of 5
You see: TypeError: Cannot read properties of undefined (reading 'name')
What type of problem caused this error?
Accessing a property on undefined or null causes TypeError. In JavaScript/TypeScript:
- TypeError — operating on a value of the wrong type (accessing property on undefined, calling something that is not a function)
- ReferenceError — referencing a variable that does not exist:
foo is not defined - SyntaxError — code that cannot be parsed:
Unexpected token - RangeError — value out of allowed range:
Maximum call stack size exceeded(infinite recursion)
.name: obj?.name or if (obj) { obj.name }.Python raises: KeyError: 'user_id'. What happened?
Accessing a dictionary key that does not exist raises KeyError in Python. Python exception categories:
- KeyError — dictionary key not found:
d['missing_key'] - IndexError — list index out of range:
lst[100]on a 3-element list - AttributeError — object has no such attribute:
obj.nonexistent_method() - ValueError — right type but invalid value:
int('hello') - TypeError — wrong type for operation:
1 + 'string'
d.get('user_id') to return None instead of raising, or check 'user_id' in d first.A Java stack trace starts with: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null. Which pattern would prevent this?
A null check before calling methods on the reference prevents NullPointerException. In Java:
- NullPointerException (NPE) — calling a method or accessing a field on a null reference
- ClassCastException — invalid cast:
(String) someObjectwhen it is not a String - IllegalArgumentException — a method received an invalid argument
- IllegalStateException — method called at the wrong time or in the wrong state
Optional<String>, or in Kotlin use the safe call operator str?.length. In Java 14+, NPEs include the null variable name in the message (as shown above).You see: ImportError: cannot import name 'Router' from 'fastapi'. What does this mean?
Router does not exist in the fastapi module at the imported location. Import errors in Python:
- ImportError: No module named 'X' — the package is not installed (
pip install X) - ImportError: cannot import name 'X' from 'Y' — the module exists but that name is not in it. Common causes: typo in the name, API change in a newer version, or the name moved to a sub-module
- ModuleNotFoundError (subclass of ImportError) — module completely absent
from fastapi import APIRouter, not Router.Which exception type is most appropriate when a function receives valid-type but out-of-range data? For example, a percentage that is 150.
ValueError — the value has the correct type but is semantically invalid. Type vs value errors:
- TypeError — wrong type: passing a string where a number is expected
- ValueError — right type, wrong value: passing 150 where only 0-100 is valid. Also:
int('hello')— string type is right for int() but the value cannot be converted
raise ValueError(f"Percentage must be between 0 and 100, got {value}"). This is important in code reviews — seeing raise TypeError when the type is correct but the range is wrong is a semantic mistake that misleads callers about what to catch.