What are different between next(error) and throw new Error in Express framework?

Viewed 97

Can someone explain to me about the different between two ways exception error handling in code Express JS below:

const express = require('express');
const app = express();

app.get('/test', (req, res, next) => {

  // the first way:
  throw new Error('my error message');

  // the second way:
  next(new Error('my error message'));

});

app.use((err, req, res, next) => {
  res.status(err.status || 500).send(err.message || 'Internal Server Error');
});

app.listen(3000, () => console.log('Welcome to ExpressJS'));

It returns the same result handled by error middleware but what is the difference here?

1 Answers
Related