Jasch1 Jasch1 - 5 months ago 7x
Javascript Question

A Reason for Extending Javascript Error Object?

As I start to utilize the javascript language to handle a lot of the heavy lifting that I used to, and still as primarily handle in java, I start to see a lot of areas where it might make sense, at least in an OO perspective, to create Errors specific to the exception. But I wonder if there is really anything to gain from creating custom error objects just to post a exception.

I know the benefits for extending the Exception class in java and I suppose those same principles apply to the javascript Error object, perhaps more so with MV* architecture, but I'm just not sure it's really worth it.

Pros & Cons: Does anyone have any thoughts on this? Any suggestions that might make someone or an application lean more towards the use of OO type Errors and also when perhaps the application would suffer from using, or the use of specific Error object wouldn't be as beneficial as just using the standard Error object?


You can create custom errors in ES6 with subclassing.

class BadArgumentTypeError extends Error {
  constructor (x, y) {
    super(`Argument Type incorrect, expected ${x} but got ${y}`);