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 ??

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

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 ???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.