Home >Web Front-end >JS Tutorial >An in-depth analysis of the try catch statement in JS and its two functions!

An in-depth analysis of the try catch statement in JS and its two functions!

青灯夜游
青灯夜游forward
2021-09-23 10:08:252594browse

This article will take you to understand the try catch statement of Javascript and introduce the two functions of the try catch statement. I hope it will be helpful to you!

An in-depth analysis of the try catch statement in JS and its two functions!

The program is executed sequentially from top to bottom. At the same time, the execution route can be changed through some control statements. Under the influence of control statements, the final execution route of the program is control flow.

The control statements in js include if, for, while, try catch, etc. They will all change the direction of the program.

Programs operate on data. The data that changes as the program runs, that is, as the control flow advances, is called data flow.

Obviously, data flow depends on control flow. Data flow analysis in program analysis also requires control flow analysis first.

For example, this piece of code:

const a = 1;
let b;

if (a === 1) {
    b = '1111';
} else {
    b = '2222';
}

Because a is 1, it will be executed until b = '1111';. This is the control flow, which is the final execution of the program. The code can be used to analyze the direction of the program and make some optimizations such as deleting dead code.

As the control flow is executed, b will be assigned a value of 2222. This is the data flow, that is, the process of changing the value, which can be used to analyze the value of the variable of a certain statement.

The program performs different processing on different data. If the data has errors, the processing program will not be able to process it, and an error will be reported, which will interrupt the subsequent control flow. For example, the data is empty, the data format is incorrect, etc. At this time, error handling must be done through try catch, also called exception handling.

We do exception handling for two purposes:

  • To do some thorough processing of the error logic.

For example, when there is an error in parameter parsing, assign a default value in the catch. After this error is handled, there is no need to report it again. In this case, try catch is also part of the logic, equivalent to if else.

  • Provide a more scenario-based description of the reported error.

JS errors are thrown by the JS engine. For example, calling a method on a null object will report a TypeError, and using an undeclared variable will report a ReferenceError. The specific Error is reported in different scenarios and has different meanings:

If this object is input from the user, then there is an error in the user input. If this object is obtained from the server Yes, that means the data returned by the server is incorrect. In different scenarios, the same Error will have more specific meanings, so we need to try catch. Then throw a custom error containing an error description with scene information.

Many libraries and frameworks do this well. The errors reported have specific scenario information and even solutions. Some are managed through error numbers, which can be managed through errorno. to find out the solution. This is a customized handling of errors.

However, many errors reported in business codes are not processed in this way, and the native Error is reported directly. We will use the exception monitoring platform to collect some errors that are thrown globally, and these errors are often relatively primitive information. Although the error location and stack are provided, we still need to look at the source code to locate the problem.

For example, an error is reported that an object is empty, but how do I know what object is empty, what is the reason, how to solve it, and whether there is a number.

It would be much better if we could catch various errors and then throw out some custom errors for specific scenarios. Third-party libraries have done a good job in this regard, but few people in business code pay attention to scenario-based custom errors.

Of course, users of the front-end business code use the software through the interface. In fact, they only need to provide some UI prompts for various errors. Since the library code is for developers, it is necessary to describe various errors in a scenario-based manner, and even number the errors and provide solutions.

But I think business code should also treat errors like third-party library code. Instead of reporting meaningless native errors, report some custom errors with specific meanings for troubleshooting and resolution. The problem will be much simpler.

However, although scenario-based custom errors can better help troubleshoot problems, they must be based on being sure of the errors that may be reported by the code. If the error message you report is different from the actual error cause, it will increase the difficulty of troubleshooting. It is better to report the original error.

Summary

The process of program execution is control flow. Affected by control statements, data will be changed during execution. The changes in data are called data flow. Control flow and Data flow is two aspects that are often analyzed in program analysis.

Errors will interrupt the control flow. We need to do some processing on the errors through try catch.

Error handling has two purposes:

One is to do some back-up processing, which is equivalent to if else, and there is no need to report the error again.

One is to describe the native JS error in a scenario, create an error object with more specific information and throw it out.

Many libraries do this very well, and even give error numbers and solutions. But in fact, a lot of business code only provides feedback to the user on the UI, and does not provide scenario-based packaging for thrown errors. This results in that the errors collected by the error monitoring platform are relatively primitive errors, and the source code needs to be viewed for troubleshooting. If you can also do some scenario-based error packaging like the library code, it will be much easier to count and troubleshoot problems, which most Javascript engineers have not done.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of An in-depth analysis of the try catch statement in JS and its two functions!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete