1. PurposeNormally, if an error occurs, the script stops immediately and prints the error to the console. With this statement, you can catch errors and perform reasonable operations, allowing the program to continue executing. 2. Grammartry { // Code... } catch (err) { //err is an object with detailed information about the error // Error capture, the above code will go to this code block when an error is reported, and will not stop running} finally { //It always executes regardless of whether an exception is thrown or caught} This statement can be nested 3. Practice If we don't know how to handle it, we The Used to throw a user-defined exception. Execution of the current function will be stopped (the statements after throw will not be executed) and control will be passed to the first Catch block in the call stack. If there is no catch block in the caller function, the program will terminate. Example: throw "Error1"; // throws an exception with a string value throw 4; // throws an exception with an integer value 4 /*There are many built-in standard error constructors in JavaScript: Error, SyntaxError, ReferenceError, TypeError, etc. We can also use them to create error objects. */ let error = new Error("Things happen o_O"); alert(error.name); // Error alert(error.message); // Things happen o_O //json exception try { JSON.parse("{ bad json o_O }"); } catch(e) { alert(e.name); // SyntaxError alert(e.message); // Unexpected token b in JSON at position 2 } Replenish: The code in the try block may throw three kinds of exceptions: Some students may not know the meaning of these three, so let me explain them here. Both of these are global objects. The global object itself does not contain any methods, but it inherits some methods through the prototype chain. The instanceof operator is used to determine the type of error:
Examples: catch (e) {//The following are the parameter properties of this object console.log(e instanceof TypeError); // true console.log(e.message); // "Describe this error" console.log(e.name); // "TypeError" console.log(e.fileName); // "The name of the file where the code that caused the exception is located" console.log(e.lineNumber); // The line number of the code that caused the exception} This concludes the article on JavaScript error handling try..catch...finally + covering throw+TypeError+RangeError. For more relevant JavaScript error handling content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: What is ssh port forwarding? What's the use?
>>: CSS realizes div completely centered without setting height
<> Operator Function: Indicates not equal t...
Problem Description html <iframe id="h5Co...
1. Cause: I need to import a sql file, but I can&...
The plugin is installed in the Firefox browser. T...
transform:scale() can achieve proportional zoomin...
Most navigation bars are arranged horizontally as...
1. CDN It is the most commonly used acceleration ...
In the front-end layout process, it is relatively...
1. Pull the official 5.0.3 image [root@localhost ...
As a required course for front-end developers, CS...
This article example shares the specific code of ...
1. Function : Allows the parent component to inse...
By default, the reading and writing of container ...
<br />Yesterday I saw at W3C that the new HT...
At work, we often need remote servers and often e...