JavaScript
Handling errors
A practical reference with examples you can run.
Introduction
Failures are part of a program, not an unusual exception to learning. Catch a specific failure near the operation that can fail. Give useful feedback and preserve enough context to recover without silently hiding the problem.
Syntax & example
try {
JSON.parse("not valid json");
} catch (error) {
console.log("Please enter valid JSON.");
}Expected output
Please enter valid JSON.
Common mistakes
- Using a variable before assigning a value to it.
- Mixing text and numbers without converting deliberately.
- Skipping an empty input or a boundary value when testing.
Practice
Change the invalid input into a valid number and compare the result.
main.jsYour ideas, in code.
Loading your editor…
Run your code to see the output here.
Ready to buildUTF-85 lines