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

381
Views
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 answers
Answer question

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 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!