Day 12 — Errors and modules
Homework
Problem 1 Part 1
function safeDivide(a, b) {
try {
if(b===0) {
throw "division by zero exception";
} else {
return a/b;
}
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 2 Part 2
}catch(err) {
console.log(err)
}
}
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 3 Part 3
console.log(safeDivide(4,2))
console.log(safeDivide(4,0))
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 4 Part 4
class NotFoundError extends Error {
constructor(messg) {
super(messg)
this.messg = "Not found error";
}
}
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 5 Part 5
function getUserById(id) {
try {
if(id != 1 && id !=2 && id!=3) {
throw new NotFoundError("id sould be 1,2 or 3")
}
return `User ${id} found`;
} catch (err) {
if(err instanceof NotFoundError) {
console.log(err.message)
}
}
}
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 6 Part 6
console.log(getUserById(1))
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).