admin管理员组文章数量:1332107
In asynchronous functions, we can simply catch the error in callback. For example :
Async func:
fs.readdir(path, function(err){
//catch error
)
As synchronous functions don't have callback, how can I catch errors?
Sync func:
fs.readdirSync(path); //throws some error
One way is to use try catch block:
try{
fs.readdirSync(path);
}
catch(err){
//do whatever with error
}
Is there any other way to do that? If yes, then which one is better?
In asynchronous functions, we can simply catch the error in callback. For example :
Async func:
fs.readdir(path, function(err){
//catch error
)
As synchronous functions don't have callback, how can I catch errors?
Sync func:
fs.readdirSync(path); //throws some error
One way is to use try catch block:
try{
fs.readdirSync(path);
}
catch(err){
//do whatever with error
}
Is there any other way to do that? If yes, then which one is better?
Share Improve this question asked May 21, 2015 at 7:36 manishmanish 9562 gold badges12 silver badges35 bronze badges 2-
3
try...catch
is the synchronous way. – elclanrs Commented May 21, 2015 at 7:38 - 1 for synchronous try ... catch is the way to handle exception – Manmay Commented May 21, 2015 at 7:43
1 Answer
Reset to default 7Is there any other way to do that?
No, that's how you do it. Typically you have all your main logic in the try
, and then just handle exceptional conditions (errors) in the catch
. (And cleanup in the finally
.)
本文标签: javascriptHow to catch errors in synchronous functions in nodejsStack Overflow
版权声明:本文标题:javascript - How to catch errors in synchronous functions in node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742273954a2444837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论