I want to create custom error message and be able to read it in my catch block, how can I achieve this? I want to make erorMessage in catch block with the text that throw Error passes and be able to display custom error message. However when I try to log (err)
const password = "test";
const repeatPassword = "test1";
let errorMessage = "";
const handleSubmit = () => {
if(password != repeatPassword) {
throw Error("Passwords must match");
}
try {
console.log("trying");
} catch(err) {
errorMessage = err;
console.log(err);
}
}
handleSubmit();
am I approaching this in wrong way?
>Solution :
just move your try above like this
const handleSubmit = () => {
try {
console.log("trying");
if(password != repeatPassword) {
throw Error("Passwords must match");
}
} catch(err) {
errorMessage = err;
console.log(err);
}
}