Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the types of JavaScript exceptions you need to know

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Need to know what JavaScript exception types, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Whether it is the browser console or the server side of Node.js, we will see JavaScript exceptions in various places. Exception handling is a necessary basic ability for writing programs. Before learning exception handling, it is very necessary to understand several types of exceptions in JavaScript.

Error

Error is the most basic error type, and all other error types inherit from this type. The Error object mainly has two important properties, message and name, which represent the error message and the error name, respectively.

The exceptions thrown during the running of the program generally have specific types, and the Error type is generally thrown by the developers themselves.

Try {throw new Error ('exception thrown by ConardLi');} catch (error) {console.log (error);}

SyntaxError-syntax error

Syntax errors are also known as parsing errors. Syntax errors are the most common type of errors in any programming language, indicating that they do not conform to the syntax specifications of the programming language.

JavaScript is an interpretive language. When executing a piece of code, you need to go through lexical analysis-> syntax analysis-> syntax tree to begin interpretation and execution:

Lexical analysis converts a character stream (char stream) into a token stream (token stream), and the parsing phase generates an abstract grammar tree (AST) from the token stream (token stream).

During these two phases, if the Javascript engine finds an unexpected / unreplaceable token, or if the token sequence is inconsistent with the expectation, the SyntaxError will be thrown.

Therefore, SyntaxError should be distinguished from other types of exceptions, which occur during JavaScript parsing / compilation. Once such exceptions occur, the entire js file cannot be executed, while other exceptions occur while the code is running. Errors of this type will cause code after the line on which the error occurs to fail to execute, but the code before that line will not be affected.

TypeError-Type error

The most common exception at runtime indicates that the variable or parameter is not of the expected type, for example, the new keyword must be followed by a constructor, and () must be preceded by a function.

ReferenceError-reference error

An error that occurs when referencing a variable that does not exist. Whenever we create or define a variable, the variable name is written to a variable storage center. This variable storage center is like key-value storage. Whenever we refer to a variable, it goes to the store to find Key and extract and return Value. If the variable we are looking for is not in the store, it throws a ReferenceError.

Note that if we call a non-existing property of an existing variable, ReferenceError will not be thrown, because the variable itself is already in storage, and calling the property that does not exist will only be an undefined state, that is, undefined:

RangeError-Boundary error

Indicates the exception that occurs when it is out of the valid range. The main situations are as follows:

The length of the array is negative or overlong

The method parameters of the numeric type are out of the predefined range

The function stack call exceeds the maximum

URIError-URL error the exception that is thrown when URL is invalid in calling a URI-related method, including encodeURI, decodeURI (), encodeURIComponent (), decodeURIComponent (), escape (), and unescape ():

Custom exception

In addition, in order to meet a variety of business requirements, in addition to the exception types already given by JavaScript, we can also customize some exception types. For example, we need to give different error prompts to users according to different exception types:

Class UnAuthError extends Error {} class ParamError extends Error {} function controller () {throw new UnAuthError ();} try {controller ();} catch (error) {if (error instanceof UnAuthError) {return'No permission';} if (error instanceof ParamError) {return 'Parameter error';}} does it help you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report