node.js net.connect() error in spite of try-catch

Viewed 833

This code is producing an error in spite of a try-catch block... It should send a error message, if the server isn't reachable and a positive message, if ist is.

try {
  net.connect(25565, "mc.hypixel.net", () => {
    console.log("connected");
  });
} catch (err) {
  console.log("not connected");
}

This is the error:

events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: connect ETIMEDOUT 104.16.78.21:25565
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  errno: -4039,
  code: 'ETIMEDOUT',
  syscall: 'connect',
  address: '104.16.78.21',
  port: 25565
}

Thank you for your help :)

1 Answers

hmm.. I believe the following answer is what net suggests

try this

net.connect(25565, "mc.hypixel.net", () => {
  console.log("connected");
})
.on('error', (err)=>console.log("not connected"));
Related