1. The following English vocabulary will appear in this article
assignment[əˈsaɪnmənt] assignment; assignment; assignment
assignment [əˈsaɪnmənt] assignment; task
call [kɔːl]
catch [kɔːt] Capture; catch; intercept; stop; stop;
constructor [kənˈstrʌktə(r)] constructor
cannot [ˈkænɒt] No
catch [kætʃ] catch; catch
constant[ˈkɒnstənt] Constant
defined [dɪˈfaɪnd] Definition
error [ˈerə(r)] Error; error; fallacy;
exceeded [ɪkˈsiːdɪd] More
function [ˈfʌŋkʃn] function
Finally [ˈfaɪnəli] Finally;
invalid [ɪnˈvælɪd] Not recognized; invalid
initializer [ɪˈnɪʃəˌlaɪzə] Initial value
left-hand [ˈleft hænd] on the left
Maximum [ˈmæksɪməm] Maximum
property [ˈprɒpəti] property; property
stack [stæk] Stack
shorthand [ˈʃɔːthænd]
syntax [ˈsɪntæks] Syntax; syntax rules; language structure;
side [saɪd] one side; one side;
reference [ˈrefrəns] talks about; involves;
range [reɪndʒ] range; boundary; interval; class; species; species;
token [ˈtəʊkən] token; tag
try [traɪ]
throw [θrəʊ] throw; throw; throw; throw; throw; throw; throw; push hard; hit hard
Uncaught
unexpected [ˌʌnɪkˈspektɪd] Unexpected; unexpected
undefined [ˌʌndɪˈfaɪnd] Undefined
variable[ˈveəriəbl] variable
2. Take you to appreciate the four common types of Errors in JS
1. ReferenceError: Undefined variable was used. The code before the error will be executed, and the code will not be executed afterwards.
// 1. Use the variable directly if it is not defined(my); // Error: Uncaught ReferenceError: my is not defined// Translation: my undefined // 2. Assign a variable to something that cannot be assigned()=1; // Error: Uncaught ReferenceError: Invalid left-hand side in assignment// Translation: The assignment on the left is invalid
2. TypeError: The variable or parameter is not the expected type, or the attribute method that does not exist in the object is called. The code before the error will be executed, and the code will not be executed afterwards.
// 1. Variables are not expected types, such as using the new command for primitive types such as strings, booleans, numerics, etc.let userName = new "zhangpeiyue"; // Error: Uncaught TypeError: "zhangpeiyue" is not a constructor// Translation: "zhangpeiyue" is not a constructor. The new operator should be a constructor // 2. Variables are not expected types, for example, variables are used as functionslet userName = "zhangpeiyue"; (userName()) // Error: Uncaught TypeError: userName is not a function// Translation: userName is not a function // 3. The object's properties or methods do not existconst obj = undefined;// null will also report an error(); // Error: Uncaught TypeError: Cannot read property 'userName' of undefined// Translation: The attribute "userName" cannot be read under undefined environment
3. RangeError: The data value is not within the range allowed by JS. The code before the error will be executed, and the code will not be executed afterwards.
// 1. The recursive function does not set the condition to jump outfunction run(){ run(); } run(); // Error: Uncaught RangeError: Maximum call stack size exceeded// Translation: Maximum call stack size exceeded. The cause function is called until the call stack limit is reached. // 2. The invalid array length should be a positive integerconst arr =new Array(-1); // Error: Uncaught RangeError: Invalid array length// Translation: Invalid array length
4. SyntaxError: That is, the code written does not comply with the js encoding rules. We can modify the error according to the following information prompts. Of course, if the syntax error is wrong, the browser will directly report an error and the entire code will not be executed.
// 1. Program errors, such as writing incorrectly, or missing, ); } These symbols.const obj = {; // Error: Uncaught SyntaxError: Unexpected token ';'// Translation: ";"The mark is somewhat unexpected. // 2. The variable definition is illegallet 8userName = "zhangpeiyue"; // Error: Uncaught SyntaxError: Invalid or unexpected token// Translation: The defined variable tag is invalid // 3. Object attribute assignment syntax errorconst obj = { userName = "zhangpeiyue" } // Error: Uncaught SyntaxError: Invalid shorthand property initializer// Translation: The initial value of the object attribute is invalid. Cause: Use "=" between an attribute in an object and its corresponding value // There are many syntax errors,I won't list them one by one here
3. Handle Error through try…catch
1. Once an Error appears in the code block wrapped by a try, the Error will be passed to the catch and the catch code block will be run. It will not affect subsequent code operation.
try{ (userName); }catch (err) { // ReferenceError: userName is not defined (err); } ("I will continue to run!!")
2. SyntaxError (synonym error) appears and will not be thrown.
try{ // Uncaught SyntaxError: Invalid or unexpected token const 8userName = "zhangpeiyue"; }catch (err) { (err); } ("I won't continue running!!")
3. Throw an error through throw new Error
try{ throw new Error("An exception occurred"); }catch (err) { // Error-related information ();// An exception occurred // Function call stack record information ();// Error: An exception occurred} ("I will continue to run!!")
4. Regardless of whether there is an exception or not, the code in finally will be executed after try and catch.
try{ throw new Error("An exception occurred"); }catch (err) { // Error-related information ();// An exception occurred // Function call stack record information ();// Error: An exception occurred}finally { // I will execute it regardless of whether there is an exception or not. Even if you have a return, I will execute it! ("I will execute it regardless of whether there is an exception or not. Even if you have a return, I will execute it!") } ("I will continue to run!!")
5. Summary
- As long as no syntax error occurs, the program can be executed without interruption.
- Using the code wrapped with try, even if there is no error, it is less efficient than the code wrapped without try.
- In a try, include as little code as possible that errors can occur.
- An error of the error type cannot be predicted in advance, and it must be captured with try catch.
- Finally, it can be omitted.
try{ //Code errors may occur}catch(err){ //Code executed only when an error occurs}finally{ //The code that must be executed regardless of whether there is an error or not}
Finally, it is not scary to have code errors. What is really scary is that problems with your business and code logic are the beginning of the real disaster!
Here is the article about the javascript code that can’t be understood? This is the end of the article that I'll try again after reading this article. For more related javaScript code errors, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!