sockets - Nodejs : How to catch exception in net. createServer.on ("data",...)?

标签 sockets node.js exception

我有一个标准的套接字服务器(无 HTTP)设置,如下(人为的):

var server = net.createServer(function(c) { //'connection' listener
  c.on('data', function(data) {
    //do stuff here
    //some stuff can result in an exception that isn't caught anywhere downstream, 
    //so it bubbles up. I try to catch it here. 
    //this is the same problem as just trying to catch this: 
    throw new Error("catch me if you can");
  });
}).listen(8124, function() { //'listening' listener
   console.log('socket server started on port 8124,');
});

现在的问题是我有一些代码抛出了根本没有被捕获的错误,导致服务器崩溃。作为最后的措施,我想在这个水平上捕获他们,但我尝试过的任何方法都失败了。

  • server.on("错误",....)
  • c.on("错误",...)

也许我需要访问套接字而不是c(连接),尽管我不确定如何实现。

我使用的是 Node 0.6.9

谢谢。

最佳答案

process.on('uncaughtException',function(err){
   console.log('something terrible happened..')
})

关于sockets - Nodejs : How to catch exception in net. createServer.on ("data",...)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12608283/

相关文章:

java - 当我尝试用 Java 保存屏幕截图时出现异常

python - 无法在Python中从客户端接收文件

node.js - express-openapi-validator:错误:TypeError:无法读取未定义的属性 'schema'

node.js - 基于Node.js的跟踪应用架构咨询

javascript - 在 Node.js 中使用循环和文件读取进行 Promise

java - 如果 try block 突然完成怎么办?

python - 是否可以在 Twisted (Python) 中使用原始套接字

c++ - 连接到自身的 POSIX 套接字客户端

c - 对非阻塞recvfrom的单次调用

java - 为什么不能在通用 catch 子句中捕获自定义异常