Last active
August 29, 2015 13:57
-
-
Save chrisslater/9526173 to your computer and use it in GitHub Desktop.
Example of exception handling
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
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