/ Published in: JavaScript
An example taken from JavaScript: The good parts showing how to catch an exception.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var sum = function(){ var value = 0; for(i=0;i<arguments.length;i++){ if(typeof arguments[i] !== 'number'){ throw { name: 'TypeError', message: 'That wasn\'t a number fool!' } } else { value += arguments[i]; } } alert(value); } var sumMe = function(){ try { sum(45,33,"string",89,12,354, true); } catch(e){ document.write(e.name + ': ' + e.message); } } sumMe();