Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the error types of javascript

Shulou Source: shulou.com Published: 2022-06-03 05:20:55 09月15日 Update

This article mainly introduces "what are the error types of javascript". In daily operation, I believe many people have doubts about what are the error types of javascript. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to answer your doubts about "what are the error types of javascript"! Next, please follow the small series to learn together!

JavaScript error types are: 1, Error;2, InternalError;3, EvalError;4, RangeError;5, ReferenceError;6, SyntaxError;7, TypeError;8, URIError.

Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

ECMS defines the following eight error types and throws different error objects when an error occurs.

Error

InternalError

EvalError

RangeError

ReferenceError

SyntaxError

TypeError

URIError

1. Error

Error is the base type from which all other error types inherit, so all error types share the same properties (all methods on error objects are methods defined by this default type). Browsers rarely throw errors of the Error type, which is primarily used by developers to throw custom errors.

For example, it is often used in interceptors/navigation guards.

II. InternalError

Errors of the InternalError type are thrown by browsers when the underlying JavaScript engine throws an exception. For example, excessive recursion leads to stack overflow. This is not the type of error that code normally handles, and if it does occur, there is probably something wrong or dangerous in the code.

III. EvalError

An EvalError type error is thrown when an exception occurs using the eval() function. ECMA-262 states,'If the eval attribute is not called directly (i.e. its name is not used as an Identifier, i.e. MemberExpression in CallExpression).'

Basically, if eval() is not treated as a function call, it will report an error.

Errors thrown by different browsers vary, but they are rarely used in this way, so they are not common.

IV. RangeError

RangeError is thrown when a value is out of bounds. For example, if an array is defined with an unsupported length, such as-20. Or if no stop condition is set for recursion.

This type does not occur much in JavaScript.

5. ReferenceError

ReferenceError occurs when an object cannot be found. (This is the cause of the famous "object expected" browser error). This error is often caused by accessing variables that do not exist.

Example:

SyntaxError

Often occurs when the string passed to eval() contains JavaScript syntax errors, which are rarely used outside eval(). This is because syntax errors in JavaScript code can cause code to fail to execute.

VII. TypeError

TypeErrors are common in JavaScript, mainly when variables are not of the expected type, or when accessing methods that do not exist, especially when using type-specific operations and variables of the wrong type.

Errors occur frequently when there is no validation before passing parameters to a function

VIII. URIError

URIError occurs only when a malformed URL is passed in using encodeURL() or decodeURL(), but it is rare because the above two functions are very robust.

At this point, the study of "what are the error types of javascript" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Types errors code functions methods browsers learning browsing variables objects differences reasons that is properties common more syntax recursion help wrong Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft Shulou Technology macOS Docker