JavaScript error handling _ javascript skills

Source: Internet
Author: User
This article mainly introduces JavaScript error handling materials. For more information, see I. error category

1. Syntax error

It is also known as parsing errors. when a traditional programming language is compiled and interpreted in JavaScript, these errors are directly caused by unexpected characters in the code, then you cannot directly compile/interpret the code. for example, a syntax error occurs when the right parenthesis is missing in a line of code. When a syntax error occurs, the code cannot be executed. In JavaScript, only the code in the same thread is affected by syntax errors. Code in other threads and other externally referenced files can be executed if it does not depend on the code that contains the error.

2. runtime error

It is also called an exception (exception, after compilation/interpreter ). At this point, the problem is not in the code syntax, but an operation that is attempted, in some cases, illegal. Eg.

Window. openMyFile ();

The browser returns an exception because the openMyFile () method does not exist. Exceptions only affect the threads that occur. other JavaScript threads can continue normal execution.

II. handling errors

1.Onerror event processing function

It is the first mechanism to assist JavaScript in error handling. When an exception occurs on the page, the error event is triggered on the window object. Eg.

The code is as follows:




Onerror example

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.