Some common mistakes in js and how to deal with them

  In the world of programming, mistakes are inevitable for everyone. It could be a careless error, it could be a logical error, it could be a grammatical error, there are reasons for all kinds of errors. For beginners in programming, some common mistakes happen from time to time. Here, some common mistakes are exposed to the 'sunshine', so that the coder can avoid it as much as possible, and add a layer of love protection to his code.
  Some common mistakes are:
  Undefined variables are actually novice programmers forgetting to write var when defining a variable, thus making the variable a global variable. In addition, the variable cannot be referenced due to the lack of understanding of the scope. one of the aspects.
  Case sensitivity. When writing code, case distinction is an aspect that novice programmers must pay attention to. Case insensitivity in js produces an error. And sometimes it's hard to find. Therefore, when writing, always pay attention to the distinction between upper and lower case.
  Unmatched braces, some statements such as for function, etc. all have braces. In order to develop good programming habits, it is best to write all braces at one time when writing braces. This way such mistakes can be avoided.
Mismatched parentheses, this error is similar to the curly braces error, and the solution is the same as for curly braces.
  Assignment is not equal. In the process of using some statements, it may be necessary to obtain equal data, but due to negligence, errors in this area may be ignored. The reason for this error is that it is not correct in js. This error is reported.
   Confuse methods and attributes. When calling a method, you must add parentheses after the method name, otherwise js will think that this is an error that only only wants methods or attributes.
  The + is not used when concatenating strings, this mistake is easy to make when concatenating strings. And the frequency of this error is very high. So in order to avoid misleading information. This is where everyone needs to pay attention.    
    After an error occurs, everyone wants to handle the error. In fact, there are two ways to handle the error:
The first is to avoid errors, and the other is to check as many errors as possible on the browser. The other is to verify the data. In fact, it is easier said than done, only on the basis of a lot of experience, errors may be reduced.
   The second is to use the statement in js.
try catch

finally

throw

These statements to check for errors
First, the try catch statement is explained.
The try statement tests a block of code for errors.
The catch statement handles errors.
The throw statement creates custom errors.
Errors are unavoidable in the world of programming;
when a JavaScript engine executes JavaScript code, various errors occur:
there may be syntax errors, which are often coding errors or typos caused by programmers.
It could be a typo or a missing feature in the language (maybe because of browser differences).
It is possible that the error is due to incorrect output from the server or the user.
Of course, it could also be due to many other unpredictable factors.
JavaScript throws errors
When an error occurs, when something goes wrong, the JavaScript engine usually stops and generates an error message.
The technical term to describe this situation is: JavaScript will throw an error, and the
try statement allows us to define blocks of code that are tested for errors when executed.
The catch statement allows us to define a block of code that is executed when an error occurs in the try block of code.
JavaScript statements try and catch are paired. The syntax of
this statement is as follows:
try
  {
  // run the code here
  }
catch(err)
  {
  // handle errors here
  }


Throw Statement
The throw statement allows us to create custom errors.
The correct technical term is: create or throw an exception.
If you use throw with try and catch, you can control program flow and generate custom error messages.
The syntax structure is as follows:

throw exception

There are some exceptions to this: JavaScript strings, numbers, logical values, or objects.
A finally clause can be included in the try catch statement. Whether or not an exception is thrown, the code block defined by it will be executed. The finally clause cannot be used alone and must be placed after the try statement.
try {
    
   ablurt ('An exception will occur');
   
}
catch (exception)
{
    alert('Exception occurred');
}
finally {
    alert('Whatever happens this line will execute');
}

The finally clause is a good place to put some cleanup code that needs to be executed regardless of whether an exception occurred earlier.
  There is also a way to fix errors with a debugger. For example, Firebug in Firefox browser, etc.










Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326932980&siteId=291194637