2017-06-28 27 views
1

fs.existsSync放在try...catch的内部是否具有实用价值?捕捉节点fs.existsSync错误

它可能会导致错误?这怎么可能发生,哪个错误会发生?

我问的原因是因为我试图避免嵌套try...catch如果可能的话。

+0

该方法会发生什么更糟糕的情况。文件不存在。似乎不合逻辑地把它放在试试看。 –

回答

2

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

fs.existsSync不能throw错误。

Here是执行fs.existsSync,其中try..catch出现自己的错误,并在发生错误时返回false