Atitit. Implementation of multiple multiple catch statements in Java script and Javascript JS exception mechanism

Source: Internet
Author: User

Atitit. Implementation of multiple multiple catch statements in Java script and Javascript JS exception mechanism

1. Syntax error (error) and run-time error (Exception)1

2. Error type judgment two ways: 1

3. The exceptions we often encounter include: 2

4.------ Code 2

5. Reference 4

1. Syntax errors (ERROR) and run-time errors (Exception)

JavaScript provides two special ways to handle errors

The BOM contains a OnError event handler, which is available on both the Window object and the Image object.

At the same time, ECMAScript defines another try...catch structure borrowed from Java to handle exceptions.

Error reporting, error handling, and code debugging have always been considered a weakness in JavaScript until the exit of ECMASCRIPT3 and ECMASCRIPT4 has improved. In ECMAScript, JS errors are divided into grammatical errors (error) and run-time errors (Exception).

Error classification

1, syntax error: Also known as parsing error, occurs in the traditional language compilation, in JavaScript occurs in the interpretation. These errors are caused directly by unexpected characters in the code and cannot be compiled/interpreted directly. When a syntax error occurs, you cannot continue executing the code. In JavaScript, only code in the same thread is affected by a syntax error. Code in other threads and other externally referenced files can continue if you do not rely on the code that contains the error.
2. Run-time error: Also known as an exception (exception, after the compile-time/interpreter). At this point, the problem is not the syntax of the code, but the attempt to complete an operation, in some cases, is illegal. Exceptions affect only the threads that occur, and other JavaScript threads can continue to perform normally.

Author:: Old Wow's paw attilax Ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

2.Error type judgment Two ways:

Method 1: Use the Name property to determine

Method 2: Pass the instanceof operator

such as: try{var scriptstr = "var a=0;var b=1;var C=a++b;alert (c)";

eval (SCRIPTSTR);

}catch (ex)

{if (ex instanceof SyntaxError)

{Alert ("Error in Syntax"); } }

3. The exceptions we often encounter include:

Evalerror: Thrown when an exception occurs with the eval () function

Rangeerror: Trigger when value exceeds range

Referenceerror: The object is not found in the case

SyntaxError: Passing a syntactically incorrect JavaScript string to the eval () function

TypeError: When an unexpected type is stored in a variable, or when accessing a method that does not exist

Urierror: When using encodeURI () or decodeURI ()andthe URI is not properly formatted

4.------Code

function Validexception (MSGX)

{

This.type= ""

This.type= "Validexception";

THIS.MSG=MSGX;

}

function Validoafa (Isalert)

{

$ (". Ati-validatebox"). each (function () {

if ($ (this). attr ("height") >height)

var ops= ($ (this). attr ("data-options"));

var ops_json=eval ("({" +ops+ "})");

if (ops_json.required)

{

if ($ (this). val () = = "")

{

if (Isalert)

alert (ops_json.missingmessage);

throw new Validexception (ops_json.missingmessage);

}

}

});

}

try{

Validoafa (TRUE);

Alert ("Other ...");

}catch (e) {

Alert (e instanceof validexception);

if (e instanceof validexception)

{

alert (e.msg);

Throw "stop process ...";

}

}

5. Reference

JavaScript exception Handling-zhen Dongtai-blog Park. htm

JavaScript Advanced Programming Reading notes (20) JS error Handling _javascript tricks _ Scripting House. htm

Introduction to JavaScript Exception handling-Navy Ice-Iteye technology website. htm

Atitit. Implementation of multiple multiple catch statements in Java script and Javascript JS exception mechanism

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.