Created
November 4, 2016 03:23
-
-
Save ryanlid/b8d8b2b982decc21d356765a2147b6fb to your computer and use it in GitHub Desktop.
异常处理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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