Skip to content

Instantly share code, notes, and snippets.

@chrisslater
Last active August 29, 2015 13:57
Show Gist options
  • Save chrisslater/9526173 to your computer and use it in GitHub Desktop.
Save chrisslater/9526173 to your computer and use it in GitHub Desktop.
Example of exception handling
var test = null;
function NumberException(message){
this.name = 'NumberException';
this.message = message
}
function isPositiveNumber(num) {
if (typeof num !== 'number')
throw new NumberException('Value entered is not a number');
return num > 0 ? true:false;
}
(function(){
var result;
try {
result = isPositiveNumber(test);
} catch(e){
console.log('exception: ' + e.message);
}
console.log('result: ', result);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment