Skip to content

Instantly share code, notes, and snippets.

@ryanlid
Created November 4, 2016 03:23
Show Gist options
  • Save ryanlid/b8d8b2b982decc21d356765a2147b6fb to your computer and use it in GitHub Desktop.
Save ryanlid/b8d8b2b982decc21d356765a2147b6fb to your computer and use it in GitHub Desktop.
异常处理
var add = function (a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw{
name: 'TypeError',
message: 'add needs numbers'
};
}
return a + b;
};
// 构造一个try_it函数,以不正确的方式调用之前的add函数。
var try_it = function () {
try {
add("seven");
} catch (e) {
document.writeln(e.name + ':' + e.message);
}
};
try_it();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment