简体   繁体   English

捕获节点fs.existsSync错误

[英]Catching Node fs.existsSync errors

Does it make practical value to put fs.existsSync inside try...catch ? fs.existsSync放入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. 我问的原因是因为我试图避免嵌套try...catch如果可能的话。

fs.existsSync cannot throw an error. fs.existsSync不能throw错误。

Here is the implementation of fs.existsSync , in which try..catch es its own errors and returns false when an error occurs. fs.existsSync的实现,其中try..catch是自己的错误,并在发生错误时返回false

Looking at the (current) implementation , it doesn't make sense to wrap it with try...catch : 看看(当前)实现 ,用try...catch包装它是没有意义的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM