Day 12 — Errors and modules
Lesson
Topic 1 Part 1
function tryExm() {
try {
const data = JSON.parse('{"name":"Priya"'); // missing closing brace
console.log(data);
} catch (err) {
console.log("Couldn't parse:", err.message);
}
console.log("App keeps running");
}
Code below mirrors ../js/lesson.js. Open DevTools → Console to see output when the script runs.
Section 2 tryExm();
//tryExm();
Code below mirrors ../js/lesson.js. Open DevTools → Console to see output when the script runs.
Section 3 Part 3
function divide(a, b) {
if (typeof a !== "number" || typeof b !== "number") {
throw new Error("Both arguments must be numbers");
}
if (b === 0) {
throw new Error("Cannot divide by zero");
}
return a / b;
}
Code below mirrors ../js/lesson.js. Open DevTools → Console to see output when the script runs.
Section 4 Part 4
try {
console.log(divide(4, 2));
console.log(divide(10, 2));
} catch (err) {
console.log("Caught:", err.message);
}
Code below mirrors ../js/lesson.js. Open DevTools → Console to see output when the script runs.