Skip to content

Instantly share code, notes, and snippets.

@AugustoPedraza
Created March 4, 2013 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AugustoPedraza/5083003 to your computer and use it in GitHub Desktop.
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".
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