I have a non-working code. I think it needs just a little mods and it should be working. I couldn't figure it out. Just started studying JS.
var add = function (a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw {
name: 'TypeError',
message: 'add needs numbers'
}
}
return a + b;
}
var try_it = function (a, b) {
try {
add(a, b);
} catch (e) {
document.writeln(e.name + ': ' + e.message);
}
}
document.writeln(try_it(2, 7));
It doesn't work. I get "undefined" error. However, if I invoke function add directly...
var add = function (a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw {
name: 'TypeError',
message: 'add needs numbers'
}
}
return a + b;
}
var try_it = function (a, b) {
try {
add(a, b);
} catch (e) {
document.writeln(e.name + ': ' + e.message);
}
}
document.writeln(add(2, 7));
... I get the desired result. There must be something wrong with function try_it?