In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about the role of the try catch sentence in JS. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The program is executed sequentially from top to bottom, and the route of execution can be changed through some control statements. under the influence of control statements, the final execution route of the program is the control flow.
The control statements in js include if, for, while, try catch and so on, which will change the direction of the program.
The program manipulates data, and the data that changes with the running of the program, that is, the progress of the control flow, is called data flow.
Obviously, the data flow depends on the control flow, and the data flow analysis in program analysis also needs to do control flow analysis first.
For example, a code like this:
Const a = 1 if (a = 1) {b = '1111;} b =' 2222;}
Because an is 1, it will be executed until b = '1111, which is the control flow, that is, the code finally executed by the program, which can be used to analyze the direction of the program and do some optimizations such as dead code deletion.
With the execution of the control flow, b is assigned a value of 2222, which 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 statement.
The program does different processing for different data, if there is an error in the data, then the processor will not be able to deal with it, it will report an error and interrupt the subsequent control flow. For example, the data is empty, the data format is not correct and so on. At this point, you have to do error handling, also known as exception handling, through try catch.
We do exception handling for two purposes:
Do some backtracking on the wrong logic.
For example, when there is an error in parameter parsing, assign a default value in catch. There is no need to report this kind of error after it has been handled. In this case, try catch is also part of the logic, the equivalent of if else.
Give a more situational description of the mistakes reported.
The error of JS is thrown by the JS engine. For example, a method that calls a null object will report TypeError, and undeclared variables will report ReferenceError. The specific Error is reported in different scenarios, so it has different meanings:
If the object is entered by the user, there is an error in the input of the user, and if the object is obtained from the server, it means that the data returned by the server is incorrect. In different scenarios, the same Error will have a more specific meaning, so we need to do try catch. Then throw a custom error containing the error description of the scene information.
Many libraries and frameworks do well at this point, and the errors reported are all specific scene information, and even solutions, and some are managed by error numbers, which can be queried through errorno. This is the custom handling of errors.
However, many errors reported in the business code do not do this kind of processing, but directly report the native Error. We will collect some throw-to-global errors through the exception monitoring platform, and these errors are often relatively primitive information, although with the error location and stack, but also by looking at the source code to locate the problem.
For example, an error is reported that the object is empty, but how do I know what the object is empty, why, how to solve it, and whether there is a number.
It would be much better if we could throw some customization errors for specific scenarios after catch various errors. This third-party library has done a good job, while few people in business code pay attention to scene-based customization errors.
Of course, the front-end business code users use the software through the interface, in fact, as long as a variety of errors to do some UI tips. And the code of the library is for developers, so it is necessary to make a scene description of various errors, or even number the errors and give solutions.
But I think business code should also treat errors like third-party library code, not to report meaningless native errors, but to report some custom errors with specific meaning, so that it will be much easier to troubleshoot and solve problems.
But while scene-based custom errors can better help troubleshoot problems, it must be based on being sure of the errors that may be reported by the code. If the error information reported by yourself is different from the actual cause of the error, it will make it more difficult to troubleshoot the problem, so it is better to report the original error.
The process of program execution is the control flow, which is affected by control statements, and the data will be changed in the process of execution. the change of data is called data flow. Control flow and data flow are the two aspects that are often analyzed in program analysis.
Errors will interrupt the control flow, we have to do some handling of the errors, through the try catch.
Error handling serves two purposes:
One is to do some background treatment, which is equivalent to if else, and there is no need to report the error again.
One is to make a scene description of the native JS error and create an error object with more specific information to throw.
Many libraries do this well and even number errors and give solutions. But in fact, a lot of the business code only gives feedback on the UI to the user, and there is no scene packaging for the errors thrown. As a result, the errors collected by the error monitoring platform are relatively primitive and need to check the source code for troubleshooting. If you can also do some scene-based error wrapper like the library code, it will be much easier to count and troubleshoot problems, which most Javascript engineers fail to do.
The above is the role of the try catch sentence in JS shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.