Estus Flask
Estus Flask

Reputation: 222865

Catching Node fs.existsSync errors

Does it make practical value to put fs.existsSync inside try...catch?

Is it possible that it will cause an error? How can this happen and which error would it be?

The reason I'm asking is because I'm trying avoid nested try...catch if possible.

Upvotes: 2

Views: 2754

Answers (2)

Pampattitude
Pampattitude

Reputation: 456

fs.existsSync cannot throw an error.

Here is the implementation of fs.existsSync, in which try..catches its own errors and returns false when an error occurs.

Upvotes: 5

robertklep
robertklep

Reputation: 203494

Looking at the (current) implementation, it doesn't make sense to wrap it with try...catch:

fs.existsSync = function(path) {
  try {
    handleError((path = getPathFromURL(path)));
    nullCheck(path);
    binding.stat(pathModule._makeLong(path));
    return true;
  } catch (e) {
    return false;
  }
};

Upvotes: 6

Related Questions