将fs.existsSync
放在try...catch
的内部是否具有实用价值?捕捉节点fs.existsSync错误
它可能会导致错误?这怎么可能发生,哪个错误会发生?
我问的原因是因为我试图避免嵌套try...catch
如果可能的话。
将fs.existsSync
放在try...catch
的内部是否具有实用价值?捕捉节点fs.existsSync错误
它可能会导致错误?这怎么可能发生,哪个错误会发生?
我问的原因是因为我试图避免嵌套try...catch
如果可能的话。
看at the (current) implementation,它没有任何意义与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
不能throw
错误。
Here是执行fs.existsSync
,其中try..catch
出现自己的错误,并在发生错误时返回false
。
该方法会发生什么更糟糕的情况。文件不存在。似乎不合逻辑地把它放在试试看。 –