javascript try catch
CH 1-14
<script>
function UserException(message) {
this.message = message;
this.name = "UserException";
}
UserException.prototype.toString = function() {
return this.name + ': "' + this.message + '"';
}
try {
var empcode = prompt(" 1.字串 \n 2.數字型態 \n 3.布林 \n 4.物件 \n 5.error", "");
if (empcode == "1") {
throw "err_1";
} else if (empcode == "2") {
throw 2;
} else if (empcode == "3") {
throw true;
} else if (empcode == "4") {
throw new UserException("user value too hight");
} else if (empcode == "5") {
throw new Error("error")
}
} catch (e) {
if (e == "err_1") {
console.log("1.字串");
} else if (e == 2) {
console.log("2.數字");
} else if (e == true) {
console.log("3.布林");
} else if (e.name == "UserException") {
console.log("4.自定義物件");
//二種寫法
console.log("第一種寫法 " + "name:" + e.name + " message:" + e.message);
console.log("第二種寫法 " + e.toString());
} else {
console.log("5.ERROR物件");
console.log(e.name + " " + e.message)
}
} finally {
}
</script>
相關參考: http://www.javascriptkit.com/javatutors/trycatch2.shtml