Last active
August 29, 2015 14:23
-
-
Save rogovski/9080e18ed7031c804e09 to your computer and use it in GitHub Desktop.
define a custom error in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error | |
| function MyError(message) { | |
| this.name = 'MyError'; | |
| this.message = message || 'Default Message'; | |
| } | |
| MyError.prototype = Object.create(Error.prototype); | |
| MyError.prototype.constructor = MyError; | |
| /* | |
| * try { | |
| * throw new MyError('custom message'); | |
| * } | |
| * catch (e) { | |
| * 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