1. If the Async method is executed synchronously, the try catch can catch an error without blocking to the main process, so Console.log (3333) can execute.
var fs = require (' FS ');
try {
var data = Fs.readfilesync (' sample.txt ', ' utf-8 ');
Console.log (data);
} catch (Err) {
Something went wrong.
Console.log (err);} Console.log (3333);
2. For some libraries without callback functions, but also asynchronous methods, it is difficult to catch the error var d = domain.create ();
D.name = ' D1 ';
Process.on (' Uncaughtexception ', function (err) {
Console.log (' Caught exception: ' + err);
});
D.on (' Error ', function (err) {
Console.log (' Domain caught to error ', err);
});
D.run (function () {
Console.log (1);
var fs = require (' FS ');
Fs.readfile (' sample.txt ', ' utf-8 ');
Console.log (2);
}
);
Console.log (3);
Result: Async method, if no callback function will throw an error after running, its subsequent programs will execute, and the synchronization method captured by domain will block the process, the subsequent method will not execute
1
2
3
Domain captures error {[Error:ENOENT:no such file or directory, open ' Sample.txt ']
errno:-2,
Code: ' ENOENT ',
Syscall: ' Open ',
Path: ' Sample.txt ',
Domain
Domain {
Domain:null,
_events: {error: [Function]},
_eventscount:1,
_maxlisteners:undefined,
Members: [],
Name: ' D1 '},
Domainthrown:true}
var d = domain.create ();
D.name = ' D1 ';
Process.on (' Uncaughtexception ', function (err) {
Console.log (' Caught exception: ' + err);
});
D.on (' Error ', function (err) {
Console.log (' Domain caught to error ', err);
});
D.run (function () {
Console.log (1);
var fs = require (' FS ');
Fs.readfilesync (' sample.txt ', ' utf-8 ');
Console.log (2);
}
);
Console.log (3333);
Result: Synchronous method with domain, if there is an error will block the process.
1
Domain captures error {[Error:ENOENT:no such file or directory, open ' Sample.txt ']
errno:-2,
Code: ' ENOENT ',
Syscall: ' Open ',
Path: ' Sample.txt ',
Domain
Domain {
Domain:null,
_events: {error: [Function]},
_eventscount:1,
_maxlisteners:undefined,
Members: [],
Name: ' D1 '},
Domainthrown:true}
Process finished with exit code 0
Deep nesting of Q