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

383
Vistas
What is the difference between throw Error(error) and throw error

What is the difference between:

try {
      const result = await hello();
    } catch (error) {
      throw error;
    }

and

try {
      const result = await hello();
    } catch (error) {
      throw Error(error);
    }

Also

Is the second one necessary? It seems like you are just wrapping an error with an Error Object. Which one is preferred? Please help me understand.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's possible that the value that the Promise rejected with was not an error object, but something else:

(async() => {
  try {
    const result = await Promise.reject(5);
  } catch (error) {
    console.log(error);
    console.log(typeof error);
  }
})();

Doing

throw Error(error);

makes sure that the value being thrown is definitely an Error object, which could be important if the thrown value is examined later and is expected to be such an object. You wouldn't want, for example, for undefined or null to be thrown (strange, I know, but not impossible) and for accessing a property of that to then throw at the point where you're catching for real.

const hello = () => new Promise((resolve, reject) => {
  reject();
});
(async() => {
  try {
    const result = await hello();
  } catch (error) {
    throw error;
  }
})()
  .catch((error) => {
    console.log('The error message was:');
    console.log(error.message);
  });

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