JS: Good parts - Ch.4 - Functions

exceptions

by Denise Nepraunig

JavaScript

/* 
All texts and infos I have taken from:
'JavaScript: The Good Parts' by Douglas Crockford
*/

// EXEPTIONS
var add = function add(a, b) {
    if (typeof a !== 'number' || typeof b !== 'number') {
        throw {
            name: 'TypeError',
            message: 'add needs numbers'
        };
    }
    return a + b;
};

var try_it = function try_it() {
    try {
        add("seven");
    } catch (e) {
        console.log(e);
    }
};

try_it();