Reputation: 1891
This is query regarding how exception is handled in node js.
Let's say there is an node + express web server. As we know it handles multiple requests in single thread - serves one request while other is waiting for IO.
Let's say while processing a request an exception will occur which is unhandled.
Upvotes: 1
Views: 1179
Reputation: 186
You could listen such errors on process
and avoid crashes:
process.on('unhandledRejection', (reason, promise) => { console.log(reason + "--" + promise); });
process.on('uncaughtException', (err) => { console.error("uncaught exception:" + err + "\n" + err.stack); });
process.on('error',(err)=>{ console.log('caught in app error listener: ' + err); });
Please note, it is not a best practice to override such listeners coz you may face with unpredictable behavior in your code.
You should always handle errors in your local methods (handle error callback, check correct type, use try...catch
, use .catch
etc...)
Upvotes: 1