7

I have this code

var fd = fs.openSync(filePath,"r");
var fr = fs.readSync(fd, buffer, 0, size, 0);

and it throws error like that

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: OK, open 'C:\Users\iahmed16\Desktop\eclipse WS\test\images\af31a9e0a98939be82f887b0005c21752e71425e.jpg'
  • how to handle this error ??
  • what's the meaning of the error if you know ??
Islam Ahmed
  • 539
  • 1
  • 5
  • 17

1 Answers1

10

The error seems to mean that you have too many file descriptions open.

You have to make sure at some point that you close() them.

var fd = fs.openSync(filePath,"r");
var fr = fs.readSync(fd, buffer, 0, size, 0);
fs.closeSync(fd);

As for how to handle the error, you can use try...catch with thrown errors:

try {
    var fd = fs.openSync(filePath,"r");
    var fr = fs.readSync(fd, buffer, 0, size, 0);
    fs.closeSync(fd);
} catch (e) {
    console.log('Error:', e);
}
Community
  • 1
  • 1
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • I'm using try...catch ... but some errors force the app to crash and not enter in the catch part. or that means I have error in another part of the code ??? – Islam Ahmed Aug 25 '13 at 10:38