gpt4 book ai didi

node.js - 无法捕获 Node opencv 错误?

转载 作者:搜寻专家 更新时间:2023-10-31 22:55:24 25 4
gpt4 key购买 nike

我正在 try catch 由 node-opencv( native 绑定(bind))抛出的错误,而不是使进程崩溃。

https://github.com/peterbraden/node-opencv

我试过使用 try{}catch(){} block 并使用 process.on('uncaughtException', function (exception) {});两者都没有任何效果,抛出错误时进程仍然崩溃。

示例:libc++abi.dylib:终止调用抛出异常

这个问题指的是抛出的任何 opencv 错误,而不是特定的错误。

我能否将我所有的 node-opencv 放入它们自己的进程中——然后将其作为我的主进程的子进程运行。当它崩溃时,重新启动它并继续?

最佳答案

你可以使用domain

Domains provide a way to handle multiple different IO operations as a single group. If any of the event emitters or callbacks registered to a domain emit an error event, or throw an error, then the domain object will be notified, rather than losing the context of the error in the process.on('uncaughtException') handler, or causing the program to exit immediately with an error code.

好吧,你不喜欢我之前的回答,所以这是另一个:

http://snmaynard.com/2012/12/21/node-error-handling/

http://benno.id.au/blog/2011/08/08/nodejs-exceptions

The other option is to use the event system provided by node.js. That is what we will look at next as it is what node.js uses internally. We are going to change our code so that y emits a myerror event rather than the exception bubbling up.

var events = require('events')
emitter = new events.EventEmitter()

function y(arg, callback) {
if (arg === 1) {
x()
callback()
} else {
function onTick() {
try {
x()
} catch(err) {
emitter.emit('myerror', err)
return
}
callback()
}
process.nextTick(onTick)
}
}

称为错误的事件由 Node 代码非常特殊地处理。来自 event.js 中的发出代码:

  if (type === 'error') {
if (!this._events || !this._events.error ||
(isArray(this._events.error) && !this._events.error.length))
{
if (arguments[1] instanceof Error) {
throw arguments[1]; // Unhandled 'error' event
} else {
throw new Error("Uncaught, unspecified 'error' event.");
}
return false;
}
}

关于node.js - 无法捕获 Node opencv 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18646547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com