最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Catching Node fs.existsSync errors - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Jun 28, 2017 at 11:45 Estus FlaskEstus Flask 223k79 gold badges472 silver badges610 bronze badges 1
  • What worse could happen with that method. File not exist. Don't seem logical to put it in try catch. – Prabodh M Commented Jun 28, 2017 at 11:51
Add a ment  | 

2 Answers 2

Reset to default 6

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;
  }
};

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.

发布评论

评论列表(0)

  1. 暂无评论