Javascript 打印node.js中捕获的异常的确切位置
抛出异常时,node.js使用^symbol提供了非常有用的精确位置。我想知道捕获异常后是否可以输出相同的内容 例如,运行的输出:Javascript 打印node.js中捕获的异常的确切位置,javascript,node.js,Javascript,Node.js,抛出异常时,node.js使用^symbol提供了非常有用的精确位置。我想知道捕获异常后是否可以输出相同的内容 例如,运行的输出: var str = '{"regex" : "[\\S]+IS"}'; JSON.parse(str); 给出: undefined:1 {"regex" : "[\S]+IS"} ^ SyntaxError: Unexpected token S at Object.parse (native) at Object.&
var str = '{"regex" : "[\\S]+IS"}';
JSON.parse(str);
给出:
undefined:1
{"regex" : "[\S]+IS"}
^
SyntaxError: Unexpected token S
at Object.parse (native)
at Object.<anonymous> (/home/bcho/tmp/exception_test.js:2:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
堆栈和消息均不包含该位置信息:
SyntaxError: Unexpected token S
at Object.parse (native)
at Object.<anonymous> (/home/bcho/tmp/exception_test_catch.js:3:10)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
Unexpected token S
捕获异常后,我可以从异常中获取位置信息吗?注意堆栈跟踪中的异常\u test\u catch.js:3:10。3:10分别表示行号和列号。@EugeneKostrikov是的,这是正确的。它指向调用JSON.parse的位置,但不是导致解析失败的实际标记。
SyntaxError: Unexpected token S
at Object.parse (native)
at Object.<anonymous> (/home/bcho/tmp/exception_test_catch.js:3:10)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
Unexpected token S