Skip to content

Latest commit

 

History

History
27 lines (23 loc) · 407 Bytes

003_0_error-handling.md

File metadata and controls

27 lines (23 loc) · 407 Bytes

Error handling

Try and Catch

const person = {
  name: 'Peter',
  age: 30,
  get personData() {
    return `${this.name} is ${this.age} old`;
  },
  set newName(name) {
    if (typeof name !== 'string') {
      throw new Error('Name must be a string');
    }
    this.name = name;
  }
}

try {
  person.newName = 1; 
} catch (err) {
  console.error(err);
}

// Error: 'Name must be a string'