


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!
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!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
