Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda