@@ -46,15 +46,35 @@ try {
4646```
4747
4848Any use of the JavaScript ` throw ` mechanism will raise an exception that
49- _ must_ be handled using ` try…catch ` or the Node.js process will exit
50- immediately.
49+ _ must_ be handled or the Node.js process will exit immediately.
5150
5251With few exceptions, _ Synchronous_ APIs (any blocking method that does not
53- accept a ` callback ` function, such as [ ` fs.readFileSync ` ] [ ] ), will use ` throw `
54- to report errors.
52+ return a {Promise} nor accept a ` callback ` function, such as
53+ [ ` fs.readFileSync ` ] [ ] ), will use ` throw ` to report errors.
5554
5655Errors that occur within _ Asynchronous APIs_ may be reported in multiple ways:
5756
57+ * Some asynchronous methods returns a {Promise}, you should always take into
58+ account that it might be rejected. See [ ` --unhandled-rejections ` ] [ ] flag for
59+ how the process will react to an unhandled promise rejection.
60+
61+ <!-- eslint-disable no-useless-return -->
62+
63+ ``` js
64+ const fs = require (' fs/promises' );
65+
66+ (async () => {
67+ let data;
68+ try {
69+ data = await fs .readFile (' a file that does not exist' );
70+ } catch (err) {
71+ console .error (' There was an error reading the file!' , err);
72+ return ;
73+ }
74+ // Otherwise handle the data
75+ })();
76+ ```
77+
5878* Most asynchronous methods that accept a ` callback ` function will accept an
5979 ` Error ` object passed as the first argument to that function. If that first
6080 argument is not ` null ` and is an instance of ` Error ` , then an error occurred
@@ -104,9 +124,9 @@ pass or fail).
104124
105125For _ all_ [ ` EventEmitter ` ] [ ] objects, if an ` 'error' ` event handler is not
106126provided, the error will be thrown, causing the Node.js process to report an
107- uncaught exception and crash unless either: The [ ` domain ` ] [ domains ] module is
108- used appropriately or a handler has been registered for the
109- [ ` 'uncaughtException' ` ] [ ] event .
127+ uncaught exception and crash unless either: a handler has been registered for
128+ the [ ` 'uncaughtException' ` ] [ ] event, or the deprecated [ ` node:domain ` ] [ domains ]
129+ module is used .
110130
111131``` js
112132const EventEmitter = require (' node:events' );
@@ -125,60 +145,6 @@ they are thrown _after_ the calling code has already exited.
125145Developers must refer to the documentation for each method to determine
126146exactly how errors raised by those methods are propagated.
127147
128- ### Error-first callbacks
129-
130- <!-- type=misc-->
131-
132- Most asynchronous methods exposed by the Node.js core API follow an idiomatic
133- pattern referred to as an _ error-first callback_ . With this pattern, a callback
134- function is passed to the method as an argument. When the operation either
135- completes or an error is raised, the callback function is called with the
136- ` Error ` object (if any) passed as the first argument. If no error was raised,
137- the first argument will be passed as ` null ` .
138-
139- ``` js
140- const fs = require (' node:fs' );
141-
142- function errorFirstCallback (err , data ) {
143- if (err) {
144- console .error (' There was an error' , err);
145- return ;
146- }
147- console .log (data);
148- }
149-
150- fs .readFile (' /some/file/that/does-not-exist' , errorFirstCallback);
151- fs .readFile (' /some/file/that/does-exist' , errorFirstCallback);
152- ```
153-
154- The JavaScript ` try…catch ` mechanism ** cannot** be used to intercept errors
155- generated by asynchronous APIs. A common mistake for beginners is to try to
156- use ` throw ` inside an error-first callback:
157-
158- ``` js
159- // THIS WILL NOT WORK:
160- const fs = require (' node:fs' );
161-
162- try {
163- fs .readFile (' /some/file/that/does-not-exist' , (err , data ) => {
164- // Mistaken assumption: throwing here...
165- if (err) {
166- throw err;
167- }
168- });
169- } catch (err) {
170- // This will not catch the throw!
171- console .error (err);
172- }
173- ```
174-
175- This will not work because the callback function passed to ` fs.readFile() ` is
176- called asynchronously. By the time the callback has been called, the
177- surrounding code, including the ` try…catch ` block, will have already exited.
178- Throwing an error inside the callback ** can crash the Node.js process** in most
179- cases. If [ domains] [ ] are enabled, or a handler has been registered with
180- ` process.on('uncaughtException') ` , such errors can be intercepted.
181-
182148## Class: ` Error `
183149
184150<!-- type=class-->
@@ -3617,6 +3583,7 @@ The native call from `process.cpuUsage` could not be processed.
36173583[ `--disable-proto=throw` ] : cli.md#--disable-protomode
36183584[ `--force-fips` ] : cli.md#--force-fips
36193585[ `--no-addons` ] : cli.md#--no-addons
3586+ [ `--unhandled-rejections` ] : cli.md#--unhandled-rejectionsmode
36203587[ `Class: assert.AssertionError` ] : assert.md#class-assertassertionerror
36213588[ `ERR_INVALID_ARG_TYPE` ] : #err_invalid_arg_type
36223589[ `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` ] : #err_missing_message_port_in_transfer_list
0 commit comments