On Sun, Jul 8, 2012 at 10:00 PM, Frederik Berg <[email protected]> wrote:
> I got the same error with node.js 0.8 (on windows 7 64 bit if it matters)
>
> And... I wrote some small script that does not fail to throw the error:
>
>>> var net = require('net');
>>>
>>> var connection;
>>>
>>>
>>> try{
>>>
>>> connection = net.connect({'port':6667,'host':'freenode.net'},function ()
>>> {
>>>
>>> console.log('connection open');
>>>
>>> });
>>>
>>> connection.setEncoding('utf8');
>>>
>>> connection.on('data', function (data) {
>>>
>>> console.dir(data);
>>>
>>> });
>>>
>>> connection.on('end', function () {
>>>
>>> console.log('irc conenction closed');
>>>
>>> });
>>>
>>> }catch(err){
>>>
>>> console.log('error: '+err);
>>>
>>> }
>
>
> it does give
>>>
>>> C:\some\path>node test.js
>>>
>>>
>>> events.js:66
>>>
>>>         throw arguments[1]; // Unhandled 'error' event
>>>
>>>                        ^
>>>
>>> Error: connect ECONNREFUSED
>>>
>>>     at errnoException (net.js:781:11)
>>>
>>>     at Object.afterConnect [as oncomplete] (net.js:772:19)
>>
>>
> If I add:
>>>
>>> process.on('uncaughtException', function(err) {
>>>
>>>   console.log(err);
>>>
>>> });
>
> The console logs:
>>
>>  { [Error: connect ECONNREFUSED]
>>
>>   code: 'ECONNREFUSED',
>>
>>   errno: 'ECONNREFUSED',
>>
>>   syscall: 'connect' }
>
>
> Now I know that the reason is, that the server is indeed refusing the
> connection but I was wondering...
> why dosn't it catch the error? why is it uncaught, even though it is inside
> a try catch statement?

You're failing to think async. Making the connection, doing the TCP
handshake, etc. are not immediate operations. In short, you need to
add an error listener to your connection object.

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to