JS Error type

Source: Internet
Author: User

Type of error

There are a number of errors that can occur when executing code. Each error has a corresponding error type, andECMA-262 defines 7 types of errors:

1.Error

2.EvalError

3.RangeError

4.ReferenceError

5.SyntaxError

6.TypeError

7.URIError

among them, Error is a base type ( Six other types of parent type ), and other types inherit from it. the Error type is rarely seen and is usually thrown by the Browser. This base type is primarily intended for developers who throw custom Errors.

PS: Throws the meaning, is the current error cannot handle, throws to another person, for example throws to a wrong object.

New Array (-5); throw rangeerror ( range )

the error message is: Rangeerror:invalid array length( Invalid array )

PS:rangeerror error usually triggers when the value goes out of range

var box = a; throw referenceerror ( reference )

the error message is: REFERENCEERROR:A is isn't defined(a is Undefined)

PS:referenceerror typically accesses non-existent variables to produce this error

A $ b; throw syntaxerror ( Syntax )

the error message is: syntaxerror:missing; Before statement(missing ; statement before )

PS:syntaxerror is usually caused by grammatical errors

New 10; throw TypeError ( type )

the error message is: Typeerror:10 is isn't a constructor( not a constructor )

PS:TypeError is usually caused by a type mismatch

PS:theevalerror type means that the global function eval () is used differently from the definition, but it does not actually produce this error, so there is little likelihood of actually encountering it.

PS: When using encodeURI () and decodeuri () , if the URI is not properly formatted, it causes Urierror Error. But because of the very strong URI compatibility, This error is almost Invisible.

Alert (encodeuri (' eon '));

Using different types of errors, you can give more appropriate error messages or Processing.

try {

New 10;

} catch (e) {

If (e instanceof TypeError) {// If the type is wrong, then do this

Alert (' A type error occurred, error message:' + e.message ');

} else {

Alert (' An unknown error has occurred! ');

}

}

Use Try-catch

it is not appropriate to use try-catch in places where it is clear that a place will produce errors that can be resolved by modifying the code . Or the kind of different browser compatibility errors that can cause errors is also inappropriate, because it is possible to resolve the browser or determine whether the browser exists with this property and Method.

try {

var box = document.getElementById (' box '); Word case error, resulting in type error

} catch (e) {/// This situation is not necessary Try-catch

Alert (e);

}

try {

Alert (innerwidth); Internet support,IE error

} catch (e) {

Alert (document.documentElement.clientWidth); compatible with IE

}

PS: General error and this browser-compatible error, we do not recommend the use of try-catch. Because the general error can be modified code to resolve, browser-compatible errors, can be judged by ordinary if . and try-catch consumes more resources and more burden than the general Statement. therefore, in the last resort, unable to modify the code, not through the general judgment of the case to use try-catch, such as the following Ajax technology.

Throw error

use catch to handle the error message, and if you can't handle it, we throw it away. Throwing an error, in fact, is to display an error message in the browser, except that the error message can be customized, more precise and specific.

try {

New 10;

} catch (e) {

If (e instanceof TypeError) {

throw new TypeError (' instantiated type causes an error! '); interpreting error messages directly in Chinese

} else {

throw new error (' throw unknown errors! ');

}

}

PS:Internet Explorer only supports error-throwing errors, and other error types are not Supported.

JS Error type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.