Skip to content

Commit 831b30e

Browse files
committed
doc: improve unhandledException doc copy
Rework the doc a bit to tighten it up, including removing the use of `you` Fix some line wrapping issues. PR-URL: #5287 Reviewed-By: Roman Klauke <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> ...
1 parent 5d7265f commit 831b30e

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

doc/api/process.markdown

+36-32
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ It is an instance of [`EventEmitter`][].
77

88
## Event: 'beforeExit'
99

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.
1314

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
1617
alternative to the `'exit'` event unless the intention is to schedule more work.
1718

1819
## Event: 'exit'
@@ -91,11 +92,12 @@ indefinitely) or upon process exit (more convenient for scripts).
9192

9293
## Event: 'uncaughtException'
9394

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.
9799

98-
Example of listening for `'uncaughtException'`:
100+
For example:
99101

100102
```js
101103
process.on('uncaughtException', (err) => {
@@ -111,26 +113,27 @@ nonexistentFunc();
111113
console.log('This will not run.');
112114
```
113115

114-
Note that `'uncaughtException'` is a very crude mechanism for exception
115-
handling.
116+
### Warning: Using `'uncaughtException'` correctly
116117

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.
120124

121125
Exceptions thrown from within the event handler will not be caught. Instead the
122126
process will exit with a non zero exit code and the stack trace will be printed.
123127
This is to avoid infinite recursion.
124128

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.
127132

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'`.
134137

135138
## Event: 'unhandledRejection'
136139

@@ -142,8 +145,8 @@ a promise chain. This event is useful for detecting and keeping track of
142145
promises that were rejected whose rejections were not handled yet. This event
143146
is emitted with the following arguments:
144147

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).
147150
- `p` the promise that was rejected.
148151

149152
Here is an example that logs every unhandled rejection to the console
@@ -254,10 +257,10 @@ Note:
254257

255258
- `SIGUSR1` is reserved by Node.js to start the debugger. It's possible to
256259
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).
261264
- `SIGPIPE` is ignored by default. It can have a listener installed.
262265
- `SIGHUP` is generated on Windows when the console window is closed, and on other
263266
platforms under various similar conditions, see signal(7). It can have a
@@ -269,11 +272,12 @@ Note:
269272
- `SIGINT` from the terminal is supported on all platforms, and can usually be
270273
generated with `CTRL+C` (though this may be configurable). It is not generated
271274
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
273277
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.
277281
- `SIGKILL` cannot have a listener installed, it will unconditionally terminate
278282
Node.js on all platforms.
279283
- `SIGSTOP` cannot have a listener installed.

0 commit comments

Comments
 (0)