Deep nesting of Q

Source: Internet
Author: User
Tags try catch

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.