我想捕获以下错误:但它没有被try catch捕获 - 因为回调是在不同的堆栈中运行的。
有没有办法模拟以下代码效果,因为我需要捕获运行子进程的范围中的错误
import { spawn } from 'child_process'
const child = spawn('python', ['plot.py', 'non/existing/file.txt'])
child.stdout.on('data', console.log.bind(console))
child.stderr.on('data', console.log.bind(console))
try{
child.on('close', function(code){
throw new Error(`some error that doesnt get caught`)
})
}
catch(e){
console.log('catch')
console.log(e)
}
答案 0 :(得分:0)
您可以在函数中添加try-catch块。
然后会捕获异常并从中找到错误。