How to catch the error of Private field must be declared in an enclosing class?

496 Views Asked by At

I have this sample code:

class Person {
  #name = "Jack";
  static hasTitle(obj) {
    try {
      console.log(obj.#age);
    } catch (error) {
      console.log(Object.keys(error));
    }

    return #name in obj;
  }
}

const p = new Person();
console.log(Person.hasTitle(p));

And I want to catch the error that occurs because age is not defined on the Person object. What happens now is that the catch block is never reached, despite wrapping the faulty line with a try/catch block. So what is the problem here?

I'm perfectly aware of the in operator in case you're wondering.

0

There are 0 best solutions below