Вопрос

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 ??
Это было полезно?

Решение

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);
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top