【问题标题】:Node js: spawn process catch exception/errorNode js:生成进程捕获异常/错误
【发布时间】:2018-05-01 06:39:36
【问题描述】:

我正在尝试使用 spawn 进程运行 c++ 可执行文件。这是可执行文件的 c++ 代码:

#include<iostream>
#include<string>
using namespace std;

void divide(int num1, int num2) {
    // Complete this function
    cout << num1/num2;

}

int main(){
    int num1, num2;
    cin >> num1 >> num2;

    divide(num1, num2);

    return 0;
}

这是nodejs的代码

var bat = spawn(filePath);

bat.stdin.write("6\n");
bat.stdin.write("0\n");

bat.stdout.on('data', function (data) {
    console.log("Output: ", data.toString());
});
bat.stderr.on('data', function (data) {
    console.log("Error: ", data.toString());
});
bat.once('exit', function (code) {
    console.log('Child exited with code ' + code);
    return;
});

我无法使用此代码捕获“除以零”异常。这段代码缺少什么?任何帮助将不胜感激。

【问题讨论】:

    标签: javascript node.js exception-handling spawn


    【解决方案1】:

    代替:

    bat.once('exit', function (code) {
    

    试试:

    bat.on('close', function (code) {
    

    【讨论】:

    • 请发布您的完整 node.js 文件。例如,在您上面发布的 sn-p 中,您缺少 spawn 的“要求”。
    猜你喜欢
    • 1970-01-01
    • 2010-09-12
    • 2015-06-11
    • 2014-03-21
    • 2010-12-14
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多