VSCode Node.js TS调试器无法在捕获的异常处中断
调试TypeScript时捕获/未捕获异常不触发断点的解决办法
调试TypeScript代码时碰到两个异常断点问题:
- 已勾选Caught Exceptions选项,但调试器不会在被try-catch捕获的异常处暂停。复现代码如下:
try { const buf = Buffer.alloc(12); buf.readInt32LE(16); } catch (e) { console.log(e); }
代码执行到console.log行时,不会在buf.readInt32LE(16)的异常触发位置中断,复杂代码场景下根本没法定位异常来源;只有未被捕获的直接抛出异常能触发调试器中断。
- 移除try-catch后,未捕获的异常也不会让调试器暂停,进程直接崩溃并在控制台输出错误信息。
当前使用的launch.json配置:
{ "version": "0.2.0", "configurations": [ { "name": "tsx", "type": "node", "request": "launch", "runtimeExecutable": "tsx", "args": ["${relativeFile}"], "cwd": "${workspaceRoot}", "internalConsoleOptions": "openOnSessionStart" } ] }
解决方案
修改launch.json配置,补充异常处理规则并显式启用sourcemap支持:
{ "version": "0.2.0", "configurations": [ { "name": "tsx", "type": "node", "request": "launch", "runtimeExecutable": "tsx", "args": ["${relativeFile}"], "cwd": "${workspaceRoot}", "internalConsoleOptions": "openOnSessionStart", "sourceMaps": true, "exceptionHandling": { "caughtExceptions": true, "uncaughtExceptions": true }, "skipFiles": [ "<node_internals>/**", "node_modules/**" ] } ] }
额外注意事项
- 确保tsx版本为最新,旧版本可能存在调试交互兼容性问题
- 调试前关闭其他调试会话,避免状态干扰
- 可同时勾选调试面板的Uncaught Exceptions选项,和配置形成双重保障
内容的提问来源于stack exchange,提问作者riv
相关产品推荐
相关产品推荐

