Return to Snippet

Revision: 30838
at August 23, 2010 20:55 by 1man


Initial Code
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();

Initial URL

                                

Initial Description
An example taken from JavaScript: The good parts showing how to catch an exception.

Initial Title
Exceptions in JavaScript

Initial Tags
javascript

Initial Language
JavaScript