Created
March 4, 2013 15:26
-
-
Save AugustoPedraza/5083003 to your computer and use it in GitHub Desktop.
Exceptions example. This example was taken from the book "Javascript: The Good Parts".
This file contains 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 add = function(a, b){ | |
var invalidTypes = (typeof a !== 'number' || typeof b !== 'number'); | |
if(invalidTypes){ | |
throw { | |
name: "TypeError", | |
message: "add needs numbers" | |
}; | |
} | |
return a + b; | |
}; | |
var tryIt = function ( ){ | |
try{ | |
add("bad input"); | |
} catch(e){ | |
console.log(e.name); | |
console.log(e.message); | |
} | |
}; | |
tryIt( ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment