Review of Error Handling in Node motivated by this article.
Specifically:
"In summary, if you’re handling async error handling, you should really use the promises catch handler, which will allow you to effectively handle the async errors. But if you’re dealing with synchronous code, the try/catch will do just fine."
I think that's not the most accurate, best, precise, or most comprehensive suggestion.
Consider:
-
Error Handling within an Asynchronous Function, Timeout, Interval, Spawn or Forked Exec. (Will likely be Synchronous internal to the Asynchronous Function and will therefore use a
try-catch
block.) -
I typically combine a
try-catch
block with aprocess.on('uncaughtException', ...)
event listener:try { process.on('uncaughtException', exception => { console.error(`Exception encountered: ${exception}`) }) //... } catch (ex) { //... }
This acts as a defacto
catch
block for anyError
that bubbles up to the top-levelprocess
itself. Even Asynchronous ones. -
Using a
try-catch
block withasync
-await
notation is (probably most-)often encountered.
Remember:
//Same syntactic and semantic functionality
Error()
new Error()
Remember that the following is invalid:
T()
.then(success => { console.log(success) }, failure => { console.log(failure) })
.catch(ex => console.error(`Line 16 - asynch catch(): ${ex.message}`))
The .catch()
chained method will handle failure
.
npm i && node main.js
Line 40 - Exception encountered: Error!
Line 16 - Exception encountered: Third Async Error!
Line 21 - asynch catch(): Third Async Error!
Line 25 - asynch catch(): Fourth Async Error - By Rejection
Line 47 - Exception encountered: Async Error!
Line 35 - UncaughtException encountered: Error: Second Async Error!
Observe that lines 54
and 28
are never logged:
- Line
28
is ignored since the prior.catch()
methods handle Exceptions, Errors, and Rejections. - Line
54
is ignored bysetTimeout()
although thetry-catch
block internal to the Timeout is used.