javaScript code is red and error is reported. Can't you understand it? Try again after reading this article

  • 2021-08-06 20:48:31
  • OfStack

1. The following English words will appear in this article

assignment [] sanm] assignment; Allocation
assignment [sanm] allocation; Mission
call [k] call
caught [k] captured; Catch; Intercept; Stop;
constructor [k [n] strkt [r]] Constructor
cannot [k & aelig; n? t] is not
catch [k & aelig; t] catch; Seize
constant [k → nst → nt] Constant
Definition of defined [d. fand]
Error in error [er] (r); Errors; Fallacy;
exceeded [k → si] > dd
function [f] k] Function
finally [fan] Ultimate; Finally
invalid [n] v & aelig; Not recognized by ld]; Invalid
Initial value of initializer [n]
left-hand [left h & aelig; nd] Left
Maximum [m & aelig; ksm] Max
property [pr ^ p ^ ti] attribute; Property
stack [st & aelig; k] Stack
shorthand [ˈʃɔːthænd]
syntax [snt & aelig; ks] Syntax; Syntactic rules; Structure;
side [sad] 1 side; 1 side;
Talking about reference [refr] [ns]; Involving;
range [rend] range; Boundary; Interval; Class; Species;
token [t] token; Mark
try [tra] Attempt
throw [θ r]; Throw; Throw; Throw; Fall; Lost; Push hard; Hit hard
Uncaught Not Captured
unexpected [nk] spektd] is unexpected; Unexpected
undefined [nd. fand] Undefined
variable [. ve] ri] Variable

2. Show you four common Error types

1. ReferenceError (reference error): An undefined variable was used. The code before the error will execute, but the code after it will not execute.


// 1 If the variable is not defined, use it directly 
console.log(my);
//  Error reporting: Uncaught ReferenceError: my is not defined
//  Translation: my Undefined 
 
// 2 That assigns a variable to the 1 Something that can't be assigned a value 
Math.random()=1;
//  Error reporting: Uncaught ReferenceError: Invalid left-hand side in assignment
//  Invalid assignment on left 

2. TypeError (type error): The variable or parameter is not of the expected type, or the property method where the object does not exist is called. The code before the error will execute, but the code after it will not execute.


// 1 The variable is not of the expected type, such as using the value of the original type such as string, Boolean value, numeric value, etc. new Orders. 
let userName = new "zhangpeiyue";
//  Error reporting: Uncaught TypeError: "zhangpeiyue" is not a constructor
//  Translation: "zhangpeiyue"  No 1 Constructor. new  Operator should be followed by 1 Constructor 
 
// 2 The variable is not of the expected type , For example, variables are used as functions 
let userName = "zhangpeiyue";
console.log(userName())
//  Error reporting: Uncaught TypeError: userName is not a function
//  Translation: userName  No 1 Functions 
 
// 3 The property or method of the object does not exist 
const obj = undefined;//  For null It will also report errors 
console.log(obj.userName);
//  Error reporting: Uncaught TypeError: Cannot read property 'userName' of undefined
//  Translation: undefined Property cannot be read under the environment of " userName " 

3. RangeError (range error): The data value is not within the allowed range of JS. The code before the error will execute, but the code after it will not execute.


// 1 The recursive function does not set the condition for jumping out 
function run(){
 run();
}
run();
//  Error reporting: Uncaught RangeError: Maximum call stack size exceeded
//  Maximum call stack size exceeded. Cause function 1 Call straight until the call stack limit is reached. 
 
// 2 Invalid array length, should be a positive integer 
const arr =new Array(-1);
//  Error reporting: Uncaught RangeError: Invalid array length
//  Invalid array length 

4. SyntaxError (Syntax Error): That is, the written code does not conform to js coding rules. We can correct the error according to the following information prompt. Of course, if the syntax is wrong, the browser will report the error directly, and the whole code will not be executed.


// 1 Program errors, such as miswriting, or missing   ,   )   ;  }  These symbols. 
const obj = {;
//  Error reporting: Uncaught SyntaxError: Unexpected token ';'
//  Translation: ";" The mark was somewhat unexpected. 
 
// 2 Illegal variable definition 
let 8userName = "zhangpeiyue";
//  Error reporting: Uncaught SyntaxError: Invalid or unexpected token
//  Invalid variable marker defined 
 
// 3 Object property assignment syntax error 
const obj = {
 userName = "zhangpeiyue"
}
//  Error reporting: Uncaught SyntaxError: Invalid shorthand property initializer
//  Invalid object property initial value. Reason: Use the "between the property in the object and its corresponding value" = " 

//  There are many grammatical errors, not here 11 Enumerate 

3. Processing Error by try … catch

1. Code Block 1 Encapsulated by try When Error appears, Error is passed to catch and the catch code block is run. It will not affect the subsequent code operation.


try{
 console.log(userName);
}catch (err) {
 // ReferenceError: userName is not defined
 console.log(err);
}
console.log(" I will continue to run! ! ")

2. SyntaxError (Syntax Error) occurs and will not be thrown.


try{
 // Uncaught SyntaxError: Invalid or unexpected token
 const 8userName = "zhangpeiyue";
}catch (err) {
 console.log(err);
}
console.log(" I'm not running anymore! ! ")

3. Throw an error through throw new Error


try{
 throw new Error(" There's been an anomaly ");
}catch (err) {
 //  Error related information 
 console.log(err.message);//  There's been an anomaly 
 //  Function call stack record information 
 console.log(err.stack);// Error:  There's been an anomaly 
}
console.log(" I will continue to run! ! ")

4. Code in finally executes after try and catch, with or without exceptions


try{
 throw new Error(" There's been an anomaly ");
}catch (err) {
 //  Error related information 
 console.log(err.message);//  There's been an anomaly 
 //  Function call stack record information 
 console.log(err.stack);// Error:  There's been an anomaly 
}finally {
 //  No matter whether there is any abnormality or not, I will execute it. Even if you have return, I will execute it, too! 
 console.log(" No matter whether there is any abnormality or not, I will execute it. Even if you have return, I will execute it, too! ")
}
console.log(" I will continue to run! ! ")

5. Summary

As long as no syntax errors occur, the program can be executed without interruption. Code that uses an try package is less efficient than code that does not use an try package, even if it is error-free. In try, contain as little code as possible that may go wrong. Errors that cannot predict the type of error in advance must be caught with try catch. finally can be omitted.

try{

 // Code where errors may occur 
}catch(err){
 // Code that executes only when an error occurs 
}finally{
 // Code that must be executed regardless of whether there is an error or not 
}

Finally, code errors are not terrible. What is really terrible is that problems in your business and code logic are the beginning of real disaster!


Related articles: