Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于定制Error类型 #200

Open
ckinmind opened this issue Nov 5, 2019 · 1 comment
Open

关于定制Error类型 #200

ckinmind opened this issue Nov 5, 2019 · 1 comment
Labels

Comments

@ckinmind
Copy link
Owner

ckinmind commented Nov 5, 2019

参考资料

@ckinmind
Copy link
Owner Author

ckinmind commented Nov 5, 2019

// 创建一个新对象,并以原型链的方式 Error 
function MyError(message) {
  this.name = 'MyError';
  this.message = message || 'Default Message';
  this.stack = (new Error()).stack;
}
MyError.prototype = Object.create(Error.prototype);
MyError.prototype.constructor = MyError;

try {
  throw new MyError();
} catch (e) {
  console.log(e.name);     // 'MyError'
  console.log(e.message);  // 'Default Message'
}

try {
  throw new MyError('custom message');
} catch (e) {
  console.log(e.name);     // 'MyError'
  console.log(e.message);  // 'custom message'
}

@ckinmind ckinmind added the Todo label Nov 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant