I am trying to create a method for generating custom error messages that can be read within my catch block. My goal is to have the errorMessage variable in the catch block contain the message that is thrown by the throw Error
function, allowing me to display a customized error message. However, I am running into issues when attempting to log the error (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();
Is my current approach incorrect?