@@ -7,12 +7,13 @@ It is an instance of [`EventEmitter`][].
7
7
8
8
## Event: 'beforeExit'
9
9
10
- This event is emitted when Node.js empties its event loop and has nothing else to
11
- schedule. Normally, Node.js exits when there is no work scheduled, but a listener
12
- for ` 'beforeExit' ` can make asynchronous calls, and cause Node.js to continue.
10
+ This event is emitted when Node.js empties its event loop and has nothing else
11
+ to schedule. Normally, Node.js exits when there is no work scheduled, but a
12
+ listener for ` 'beforeExit' ` can make asynchronous calls, and cause Node.js to
13
+ continue.
13
14
14
- ` 'beforeExit' ` is not emitted for conditions causing explicit termination, such as
15
- [ ` process.exit() ` ] [ ] or uncaught exceptions, and should not be used as an
15
+ ` 'beforeExit' ` is not emitted for conditions causing explicit termination, such
16
+ as [ ` process.exit() ` ] [ ] or uncaught exceptions, and should not be used as an
16
17
alternative to the ` 'exit' ` event unless the intention is to schedule more work.
17
18
18
19
## Event: 'exit'
@@ -91,11 +92,12 @@ indefinitely) or upon process exit (more convenient for scripts).
91
92
92
93
## Event: 'uncaughtException'
93
94
94
- Emitted when an exception bubbles all the way back to the event loop. If a
95
- listener is added for this exception, the default action (which is to print
96
- a stack trace and exit) will not occur.
95
+ The ` 'uncaughtException' ` event is emitted when an exception bubbles all the
96
+ way back to the event loop. By default, Node.js handles such exceptions by
97
+ printing the stack trace to stderr and exiting. Adding a handler for the
98
+ ` 'uncaughtException' ` event overrides this default behavior.
97
99
98
- Example of listening for ` 'uncaughtException' ` :
100
+ For example :
99
101
100
102
``` js
101
103
process .on (' uncaughtException' , (err ) => {
@@ -111,26 +113,27 @@ nonexistentFunc();
111
113
console .log (' This will not run.' );
112
114
```
113
115
114
- Note that ` 'uncaughtException' ` is a very crude mechanism for exception
115
- handling.
116
+ ### Warning: Using ` 'uncaughtException' ` correctly
116
117
117
- Do * not* use it as the Node.js equivalent of ` On Error Resume Next ` . An
118
- unhandled exception means your application - and by extension Node.js itself -
119
- is in an undefined state. Blindly resuming means * anything* could happen.
118
+ Note that ` 'uncaughtException' ` is a crude mechanism for exception handling
119
+ intended to be used only as a last resort. The event * should not* be used as
120
+ an equivalent to ` On Error Resume Next ` . Unhandled exceptions inherently mean
121
+ that an application is in an undefined state. Attempting to resume application
122
+ code without properly recovering from the exception can cause additional
123
+ unforeseen and unpredictable issues.
120
124
121
125
Exceptions thrown from within the event handler will not be caught. Instead the
122
126
process will exit with a non zero exit code and the stack trace will be printed.
123
127
This is to avoid infinite recursion.
124
128
125
- Think of resuming as pulling the power cord when you are upgrading your system.
126
- Nine out of ten times nothing happens - but the 10th time, your system is bust.
129
+ Attempting to resume normally after an uncaught exception can be similar to
130
+ pulling out of the power cord when upgrading a computer -- nine out of ten
131
+ times nothing happens - but the 10th time, the system becomes corrupted.
127
132
128
- ` 'uncaughtException' ` should be used to perform synchronous cleanup before
129
- shutting down the process. It is not safe to resume normal operation after
130
- ` 'uncaughtException' ` . If you do use it, restart your application after every
131
- unhandled exception!
132
-
133
- You have been warned.
133
+ The correct use of ` 'uncaughtException' ` is to perform synchronous cleanup
134
+ of allocated resources (e.g. file descriptors, handles, etc) before shutting
135
+ down the process. It is not safe to resume normal operation after
136
+ ` 'uncaughtException' ` .
134
137
135
138
## Event: 'unhandledRejection'
136
139
@@ -142,8 +145,8 @@ a promise chain. This event is useful for detecting and keeping track of
142
145
promises that were rejected whose rejections were not handled yet. This event
143
146
is emitted with the following arguments:
144
147
145
- - ` reason ` the object with which the promise was rejected (usually an [ ` Error ` ] [ ]
146
- instance).
148
+ - ` reason ` the object with which the promise was rejected (usually an
149
+ [ ` Error ` ] [ ] instance).
147
150
- ` p ` the promise that was rejected.
148
151
149
152
Here is an example that logs every unhandled rejection to the console
@@ -254,10 +257,10 @@ Note:
254
257
255
258
- ` SIGUSR1 ` is reserved by Node.js to start the debugger. It's possible to
256
259
install a listener but that won't stop the debugger from starting.
257
- - ` SIGTERM ` and ` SIGINT ` have default handlers on non-Windows platforms that resets
258
- the terminal mode before exiting with code ` 128 + signal number ` . If one of
259
- these signals has a listener installed, its default behavior will be removed
260
- (Node.js will no longer exit).
260
+ - ` SIGTERM ` and ` SIGINT ` have default handlers on non-Windows platforms that
261
+ resets the terminal mode before exiting with code ` 128 + signal number ` . If
262
+ one of these signals has a listener installed, its default behavior will be
263
+ removed (Node.js will no longer exit).
261
264
- ` SIGPIPE ` is ignored by default. It can have a listener installed.
262
265
- ` SIGHUP ` is generated on Windows when the console window is closed, and on other
263
266
platforms under various similar conditions, see signal(7). It can have a
@@ -269,11 +272,12 @@ Note:
269
272
- ` SIGINT ` from the terminal is supported on all platforms, and can usually be
270
273
generated with ` CTRL+C ` (though this may be configurable). It is not generated
271
274
when terminal raw mode is enabled.
272
- - ` SIGBREAK ` is delivered on Windows when ` CTRL+BREAK ` is pressed, on non-Windows
275
+ - ` SIGBREAK ` is delivered on Windows when ` CTRL+BREAK ` is pressed, on
276
+ non-Windows
273
277
platforms it can be listened on, but there is no way to send or generate it.
274
- - ` SIGWINCH ` is delivered when the console has been resized. On Windows, this will
275
- only happen on write to the console when the cursor is being moved, or when a
276
- readable tty is used in raw mode.
278
+ - ` SIGWINCH ` is delivered when the console has been resized. On Windows, this
279
+ will only happen on write to the console when the cursor is being moved, or
280
+ when a readable tty is used in raw mode.
277
281
- ` SIGKILL ` cannot have a listener installed, it will unconditionally terminate
278
282
Node.js on all platforms.
279
283
- ` SIGSTOP ` cannot have a listener installed.
0 commit comments