class OutOfFuelError extends Error { constructor(message) { super(message) this.name = "OutOfFuelError" } } class FlatTireError extends Error {} try { const car = new Car() //imagine we have a Car object if (!car.fuel) { throw new OutOfFuelError('No fuel!') } if (car.flatTire) { throw new FlatTireError('Flat tire!') } } catch (err) { if (err instanceof OutOfFuelError) { //handle error } else if (err instanceof FlatTireError) { //handle error } } // from https://flaviocopes.com/javascript-custom-errors/