“This is my fourth day of the November Gwen Challenge. Check out the details: [Last Gwen Challenge 2021]

preface

It’s not uncommon for programmers to encounter problems, and often we can suffer for a long time because of a bug. I also summarized some methods and ideas of how to solve problems as front-end workers, in fact, I mainly provide ideas, because the problem is endless, only master the idea, in order to find a way out of the multifarious problems, so as to solve the problem. It is better to teach a man to fish than to give him a fish. Today I dare to be a fisherman.

1. Error handling policy

In the past, the error handling strategy for Web applications was basically landing on the server. Error handling strategies involve many error and error handling considerations, including logging and monitoring systems. The main purpose of this is to analyze patterns in order to find the root of the problem and understand how many users are affected by the error. It is also important to implement error handling strategies at the JavaScipt level of your Web application. Because any JavaScript error can render a web page unusable, it is important to understand when and why these errors occur. The vast majority of Web application users are technically illiterate and often confused when a page goes wrong. To solve the problem, they may try to refresh the page, or they may simply give up. As a developer, you should be very aware of the circumstances in which your code will fail and the consequences of failure. In addition, a system should be in place to track these problems.

2. Identify errors

A very important part of error handling is first identifying where errors are likely to occur in your code. Because JavaScript is loosely typed and does not validate function arguments, many errors occur only when the code is actually running. In general, there are three types of errors to watch out for:

  • Type conversion error
  • Data type error
  • Communication error

3. Try/catch statements

Ecma-262 Version 3 added the Trylcatch statement as a way to handle exceptions in JavaScript. The basic syntax is the same as for trylcatch statements in Java:

 {
 // Possible error code is not clear
 catch ( error ){
// What to do when an error occurs
// Any code that can go wrong should be placed in a try block, and any code that can handle an error should be placed in a catch block, as follows:
try {
 window . someNonexistentFunction ( O ;) catch ( error ){
 console . log ("An error Happened!" );Copy the code

If something in the try block fails, the code immediately exits execution and jumps to the catch block. The catch block then receives an object containing information about the error that occurred. Unlike other languages, an error object must be named even if it is not used in a catch block. The actual information exposed in the error object varies from browser to browser, but at a minimum contains the Message property that holds the error message. Ecma-262 also specifies the name attribute that defines the error type, which is currently available in all browsers.

conclusion

I think there are many ways to deal with bugs, such as breakpoints, printing and break, etc. The most important thing is to learn and use them flexibly, to deal with problems “according to local conditions”, to think more, and to persist, you can also go out for a walk, and maybe come back to figure it out. Anyway, come on! Ha ha!