Have you tried listening for "exit" in addition to "SIGINT" and
"uncaughtException"?
process.on('exit', (code) => {
// do something
});
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of
Atnakus Arzah
Sent: Tuesday, March 22, 2016 10:30 PM
To: nodejs
Subject: Re: [nodejs] process.on('uncaughtException') doesn't work everytime?
On Wed, Mar 16, 2016 at 04:40:57PM -0700, William Lin wrote:
>Hello,
>I put this in my code:
>process.on('uncaughtException', function(err){
> console.error(err.stack);
> console.log("Node NOT Exiting...");
>});
>If I do something like
>nonExistingFunction();
>,then it will catch the error, but my script always stops with the
>error
>events.js:141
> throw err;
>something like that.
>
>Can anyone help me?
>Thank you.
>
I tried with the below code. When I press CTRL-C, the SIGINT is trapped and it
calls an undefined function, which then results in the uncaughtException part
of the code to execute. Note that if you have nonExistingFunction() at the top
level then the node compiler will detect it before it has a chance to execute.
setInterval(function () { console.log("hello I am alive");}, 1000);
process.on('SIGINT', function() {
console.log('Got SIGINT. Calling a nonExistingFunction');
nonExistingFunction();
});
process.on('uncaughtException', function(err){
console.log("Okay caught an unhandled exception");
// console.error(err.stack);
process.exit(-1);
});
Below are the results of the run:
hello I am alive
hello I am alive
hello I am alive
hello I am alive
^CGot SIGINT. Calling a nonExistingFunction Okay caught an unhandled exception
--
Atnakus Arzah <[email protected]>
When in doubt, have a cookie!
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/20160323023010.GA5069%40Sukantas-MacBook-Pro.local.
For more options, visit https://groups.google.com/d/optout.
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/56f2a1b7.d3d80d0a.19b8d.5729%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.