[ Web Proxy ]
URL:
Viewing: https://javascript.info/task/format-error [Back]  [Original]

Inherit from SyntaxError
EN

We want to make this open-source project available for people all around the world.

Help to translate the content of this tutorial to your language!

    Search on Javascript.info:
    Search in the tutorial:
    Light themeDark theme
    DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
    back to the lesson

    Inherit from SyntaxError

    importance: 5

    Create a class FormatError that inherits from the built-in SyntaxError class.

    It should support message, name and stack properties.

    Usage example:

    let err = new FormatError("formatting error");
    
    alert( err.message ); // formatting error
    alert( err.name ); // FormatError
    alert( err.stack ); // stack
    
    alert( err instanceof FormatError ); // true
    alert( err instanceof SyntaxError ); // true (because inherits from SyntaxError)
    solution
    class FormatError extends SyntaxError {
      constructor(message) {
        super(message);
        this.name = this.constructor.name;
      }
    }
    
    let err = new FormatError("formatting error");
    
    alert( err.message ); // formatting error
    alert( err.name ); // FormatError
    alert( err.stack ); // stack
    
    alert( err instanceof SyntaxError ); // true

    Web Proxy Viewer  |  New URL  |  Original Page