【发布时间】:2021-05-19 16:20:19
【问题描述】:
我有两 (2) 个执行相同操作的代码,但其中一个 (Code01) 在使用 vscode 中的调试器运行时显示未捕获的异常,而另一个则不显示 (Code02)。
有人可以帮我理解其中的区别吗?
// ./vscodeDebuggerAsyncCatchExceptionTest01.js
// "Code01" - This code has an uncaught exception.
"use strict";
async function thisThrows() {
throw new Error("Thrown from thisThrows()"); // uncaught exception here
}
async function myFunctionThatCatches() {
return await thisThrows().catch((e) => {
throw e;
});
}
async function run() {
await myFunctionThatCatches().catch((e) => {
throw e;
});
}
run()
.catch( e => {
console.error(e);
console.error("++++ ERROR was caught");
})
// ./vscodeDebuggerAsyncCatchExceptionTest02.js
// "Code02" - This code runs well. All exception are handled.
"use strict";
async function thisThrows() {
throw new Error("Thrown from thisThrows()");
}
async function myFunctionThatCatches() {
return await thisThrows().catch((e) => {
throw e;
});
}
async function run() {
try {
await myFunctionThatCatches().catch((e) => {
throw e;
});
} catch (e) {
throw e;
}
}
run()
.catch( e => {
console.error(e);
console.error("++++ ERROR was caught");
})
这是“Code02”vscode 调试器输出:
发生异常:错误:从 thisThrows() 抛出 这抛出 (d:\JavaProjects\JavaScript\LWPS01\libJS\LoProbe\vscodeDebuggerAsyncCatchExceptionTest01.js:7:9) 在 myFunctionThatCatches (d:\JavaProjects\JavaScript\LWPS01\libJS\LoProbe\vscodeDebuggerAsyncCatchExceptionTest01.js:11:16) 运行时 (d:\JavaProjects\JavaScript\LWPS01\libJS\LoProbe\vscodeDebuggerAsyncCatchExceptionTest01.js:18:9) 在对象。 (d:\JavaProjects\JavaScript\LWPS01\libJS\LoProbe\vscodeDebuggerAsyncCatchExceptionTest01.js:23:1) 在 Module._compile (internal/modules/cjs/loader.js:1063:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) 在 Module.load (internal/modules/cjs/loader.js:928:32) 在 Function.Module._load (internal/modules/cjs/loader.js:769:14) 在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) 在 internal/main/run_main_module.js:17:47
【问题讨论】:
-
如果你在没有调试器的情况下运行“code01”,你会得到一个异常吗?您正在比较不同条件下的两组不同代码。
-
嗨,我同意你的看法。当我在没有调试器的情况下运行这两个代码时,一切看起来都很好。但是,当我使用调试器运行这两个代码时,无法重现“Code02”行为。看起来“Code02”没有沉默。这是我想了解的问题。
标签: javascript unhandled-exception uncaught-exception