JavaScript Error Handling
Plans from $25/month

This lesson is for members. Join us?

Subscribe now to get instant access to this course, plus a full library of Vue.js courses tailored to the plan you choose.

JavaScript Custom Error Classes: When, Why, How?

In this lesson, learn how to create a custom JavaScript error type. It’s as simple as extending the base Error type like this:

class PaymentError extends Error{
    constructor(code, message){
        super(message);
        this.name = "PaymentError"
    }
}

These custom error classes can then extended with more app specific functionality like introducing app specific error codes and related user friendly error messages. These custom error classes can also be detected with the instanceof keyword to handle different error types in different ways in catch blocks.