PrefaceThe Javascript engine is single-threaded, so once an exception is encountered, the Javascript engine will usually stop executing, block subsequent code and throw an exception message. Therefore, for foreseeable exceptions, we should capture them and display them correctly to users or developers. Error ObjectWhen a runtime error occurs, an instance of Error will be thrown. The error object has two properties:
Creating an Error
Error type js defines the following 7 error types:
throwSome JavaScript codes have no syntactic errors, but there are logical errors. For such errors, JavaScript will not throw an exception. At this time, we can define an instance of the error object ourselves and use the throw statement to actively throw an exception. In the program, we can purposefully throw exceptions by using the throw statement. Its syntax is as follows:
try…catch…finally
There are three forms of try statements:
The finally ruleWhen an exception is thrown in the finally block, the exception in the try block will be overwritten. try { try { throw new Error('can not find it1'); finally throw new Error('can not find it2'); } } catch (err) { console.log(err.message); } // can not find it2 If you return a value from the finally block, that value will become the return value of the entire try-catch-finally, regardless of whether there are return statements in the try and catch. This includes exceptions thrown in catch blocks. function test() { try { throw new Error('can not find it1'); return 1; } catch (err) { throw new Error('can not find it2'); return 2; finally return 3; } } console.log(test()); // 3 Try / Catch PerformanceA well-known anti-optimization pattern is to use try/catch In V8 (and possibly other JS engines), functions using try/catch statements cannot be optimized by the V8 compiler. window.onerrorBy defining an event listener function on window.onerror, uncaught exceptions generated by other code in the program will often be caught by the listener function registered on window.onerror
Exceptions in PromisesException thrown in Promise
Catching exceptions in Promise
Notice In a JavaScript function, only return / yield / throw will interrupt the execution of the function, and reject will not prevent further execution. Example: Reject without return Promise.resolve() .then(() => { console.log('before execute reject'); reject(new Error('throw error')); console.log('after execute reject'); }) .catch((err) => { console.log(err.message); }); // before execute reject // throw error // after execute reject Reject using return Promise.resolve() .then(() => { console.log('before execute reject'); return reject(new Error('throw error')); console.log('after execute reject'); //*** The difference is here, if return is returned, it will not be executed here}) .catch((err) => { console.log(err.message); }); // before execute reject // throw error Vue exception captureVue.config.errorHandler = (err, vm, info) => { console.error("Error captured by vue errorHandler"); console.error(err); console.error(vm); console.error(info); }; SummarizeThis is the end of this article about the error catching mechanism for JavaScript basics. For more relevant js error catching mechanism content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Dynamic SQL statement analysis in Mybatis
>>: Linux Check the installation location of the software simple method
It is standard for websites to enable SSL nowaday...
Uninstall the system-provided MySQL 1. Check whet...
Preface I am used to writing less/sass, but now I...
React Lifecycle Two pictures to help you understa...
As Web developers, although we are not profession...
Writing XHTML demands a clean HTML syntax. Writing...
Execution problem between mysql max and where Exe...
This article example shares the specific code of ...
Here we take the Jenkins container as an example ...
In the previous article, I introduced the detaile...
Table of contents introduction Cookie What are Co...
Preface When we write code, we occasionally encou...
Preface Recently, I accidentally discovered MySQL...
Log rotation is a very common function on Linux s...
This article mainly introduces: using Vue to impl...