In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what are the types of errors in JavaScript, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
JavaScript program error types can be divided into: 1, Error;2, EvalError;3, RangeError;4, ReferenceError (citation error), 5, SyntaxError (syntax error), 6, TypeError;7, URIError.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Error types in JavaScript
ECMA-262 defines seven types of errors:
Error
EvalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
Where Error is the base type (the parent of the other six types), and the other types inherit from it. The Error type is rare and is usually thrown by the explorer. This base type is mainly used by developers to throw custom errors.
1. SyntaxError (syntax error):
1.1 variable name does not conform to the specification
Var 1 / / Uncaught SyntaxError: Unexpected numbervar 1a / / Uncaught SyntaxError: Invalid or unexpected token
1.2 assign values to keywords
Function = 5 / / Uncaught SyntaxError: Unexpected token =
2. Uncaught ReferenceError (quote error):
An error that occurs when referencing a variable that does not exist. Assign a value to an object that cannot be assigned, such as to the running result of a function or to a function.
2.1 references to variables that do not exist
A (); / / Uncaught ReferenceError: an is not definedconsole.log (b); / / Uncaught ReferenceError: b is not defined
2.2 assign a value to an object that cannot be assigned
Console.log ("abc") = 1 / / Uncaught ReferenceError: Invalid left-hand side in assignment
3. RangeError (range error):
RangeError is an error that occurs when only one is out of the valid range. There are several main cases, the first is that the array length is negative, the second is that the method parameters of the Number object are out of range, and the function stack exceeds the maximum value.
3.1 Array length is negative
[] .length =-5; / / Uncaught RangeError: Invalid array length
3.2 the method parameters of the Number object are out of range
Var num = new Number; console.log (num.toFixed (- 1)); / / Uncaught RangeError: toFixed () digits argument must be between 0 and 20 at Number.toFixed// indicates that the function of the toFixed method is to round the number to the specified number of decimal places, and the parameter is the number of places after the decimal point, ranging from 0 to 20.
4. TypeError (incorrect type):
An error that occurs when a variable or parameter is not of the expected type. For example, using primitive types such as new strings and Boolean values and methods that call objects that do not exist will throw this error because the argument to the new command should be a constructor.
4.1 call a method that does not exist
123 (); / / Uncaught TypeError: 123 is not a functionvar o = {}; o.run (); / / Uncaught TypeError: o.run is not a function
4.2 new keyword followed by basic type
Var p = new 456 (); / / Uncaught TypeError: 456 is not a constructor
5. URIError (URL error):
The main reason is that the parameters of the related function are incorrect.
DecodeURI ("%"); / / Uncaught URIError: URI malformed at decodeURI
: tip reminder: errors thrown when the relevant parameters of URI are incorrect, mainly involving six functions: encodeURI, decodeURI (), encodeURIComponent (), decodeURIComponent (), escape () and unescape (). :::
6. EvalError eval () (function execution error):
In JavaScript below ES5, an evalError error is thrown when the eval () function is not executed correctly. For example, the following situation:
Var myEval = eval;myEval ("alert ('call eval')"); note that this error is no longer thrown in JavaScript above ES5, but this type of error prompt can still be customized through the new keyword.
: warning make good use of try-catch it is not suitable to use try-catch where you know that errors will occur somewhere and can be solved by modifying the code. Or it is not appropriate that different browser compatibility errors lead to errors, because it can be solved by judging the browser or determining whether this property and method exists in the browser. :::
New Error ("error!") ; new RangeError ("error, variable is out of valid range!") ; new TypeError ("error, invalid variable type!")
The above code represents the creation of an instance of the error object, which is essentially throwing the error manually. As you can see, the constructor of the error object takes a parameter that represents the error message (message).
Familiar with these basic types of errors, you can quickly locate and solve problems when errors occur.
The above is all the content of the article "what are the types of errors in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.