| Previous | Notes index | Next |
|---|---|---|
| Previous: Strings, numbers, dates, and regular expressions | Notes index | Next: The DOM and page updates |
An error is information that something went wrong. JavaScript can report syntax errors before a program runs, runtime errors while it runs, and a program can also produce an incorrect result without throwing any error. Good debugging starts by identifying which kind of problem you have.
Check ordinary user input at the boundary where it enters the program. A normal validation result is often easier to handle than throwing an exception for an expected condition.
function parseQuantity(input) {
const quantity = Number(input);
if (!Number.isInteger(quantity) || quantity < 1) {
return { ok: false, message: "Enter a whole number greater than zero." };
}
return { ok: true, value: quantity };
}
console.log(parseQuantity("3"));
console.log(parseQuantity("0"));This function returns an explicit result for success or failure. The caller can show the message or continue with the value.
Use throw when an operation cannot continue according to its contract. A thrown value should usually be an Error object because it includes a name, message, and stack trace.
function getRequiredMember(members, id) {
const member = members.find((item) => item.id === id);
if (!member) {
throw new Error("Member was not found");
}
return member;
}
try {
const member = getRequiredMember([], 7);
console.log(member.name);
} catch (error) {
console.error(error.message);
}Catch an error only when the current code can handle it or add useful context. Otherwise, let it reach a layer that can make the right decision.
A finally block runs after try and catch whether the operation succeeded or failed. It is useful for cleanup that must always happen.
let isBusy = true;
try {
console.log("Working");
} catch (error) {
console.error(error);
} finally {
isBusy = false;
}
console.log(isBusy);For browser requests, update a loading state in finally so the interface does not remain busy after a failed request.
A custom error class gives callers a clear way to recognize a specific failure while retaining standard error details.
class MissingMemberError extends Error {
constructor(memberId) {
super("Member " + memberId + " was not found");
this.name = "MissingMemberError";
}
}
try {
throw new MissingMemberError(7);
} catch (error) {
if (error instanceof MissingMemberError) {
console.log(error.message);
} else {
throw error;
}
}Do not catch every error and return an empty value. That hides the reason the operation failed and can make later behavior confusing.
In a browser, open developer tools and check the Console for error messages. The Sources panel lets you place a breakpoint, pause execution, inspect local variables, and step through code line by line.
function calculateAverage(values) {
const total = values.reduce((sum, value) => sum + value, 0);
return total / values.length;
}
console.log(calculateAverage([4, 8]));
console.log(calculateAverage([]));The second result is not a thrown error, but it is not a useful average either. Decide what an empty input should mean, then validate or handle it explicitly.
Use console.log for a quick check. Use console.table for a small collection of records. Remove temporary logs once the issue is fixed, or replace them with intentional application logging.
- Reproduce the issue with the smallest input that still fails.
- Read the first useful error and its file and line.
- Inspect the values before the failing operation.
- Check assumptions about types, missing values, and array length.
- Change one thing, rerun the case, and confirm the expected result.
- Add a regression check for the behavior if it is likely to return.
- Syntax errors prevent code from parsing. Runtime errors occur while code runs.
- Incorrect logic may produce a wrong value without throwing.
- Validate expected input and reserve exceptions for exceptional failures.
- Catch an error only when you can handle it or add context.
- Use finally for cleanup that must always happen.
- Breakpoints and small reproducible cases make debugging more reliable.
- How does a logic error differ from a runtime error?
- When should input validation happen?
- What kind of value should throw usually receive?
- When is a catch block useful?
- When does finally run?
- Why might a custom error class help?
- Which browser DevTools panel can pause at a breakpoint?
- Why is returning an empty value after every error risky?