Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created May 11, 2014 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miguelmota/5306c6b83b9323e808c7 to your computer and use it in GitHub Desktop.
Save miguelmota/5306c6b83b9323e808c7 to your computer and use it in GitHub Desktop.
Custom error type in JavaScript
function MyError(message) {
this.name = 'MyError';
this.message = message;
this.stack = (new Error()).stack;
}
MyError.prototype = new Error();
MyError.prototype.constructor = MyError;
try {
throw new MyError("custom message");
} catch (e) {
if (e instanceof MyError) {
console.log(e.name); // "MyError"
console.log(e.message); // "custom message"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment