【发布时间】:2019-08-26 23:41:09
【问题描述】:
我在一个整数变量上使用 array.pop() 函数并期待一个错误。 目前,我收到“TypeError:x.pop 不是函数”消息。 我想使用“throw”用我自己的消息覆盖它
我尝试在第一个 catch 块中使用另一个 try-catch,这样就完成了工作。但我想在第一个 try 块本身中覆盖第一个 TypeError 异常。
let x = 3
try {
x.pop();
// I want to override the exception generated due to this line
// with my own error message using throw
}
catch (e) {
try {
throw thisErr("this is my custom error message..")
}
catch (er) {
console.log(er);
}
}
function thisErr(message) {
let moreInfo = message
let name = "My Exception"
return `${name}: "${moreInfo}"`
}
我期待My Exception: "this is my custom error message.."
【问题讨论】:
标签: javascript error-handling try-catch throw