Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
Pass custom message to catch error block with throw error

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);
      }
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!